1、Vuex

1、为什么使用VueX

data从最上面的组件,一层层往下传值,一层层的验证

Vue单向数据流

“中央空调“,代理

VueX 解决数据 传值。。


2、Vuex介绍与安装

(1)Vuex官网

Vuex官网 https://vuex.vuejs.org/zh/installation.html

  

Vue单向数据流

 

(2)安装

VueX安装:https://vuex.vuejs.org/zh/installation.html

3、Vuex的使用

data在store中state

main.js 代码

import Vue from 'vue'
import App from './App' import router from './router'
Vue.config.productionTip = false // 1.0 引用vuex
import Vuex from "vuex"
Vue.use(Vuex); // 1.1 创建sotre
// 如果在模块化构建系统中,请确保在开头调用了 Vue.use(Vuex)
const store = new Vuex.Store({
state: { allList:[] // 后端的数据保存在state中
// state 这里面的状态跟每个组件的数据属性有关系 },
mutations: { }
}) /* eslint-disable no-new */
new Vue({
el: '#app',
router,
store, // 1.3 引入store
components: { App },
template: '<App/>'
})

4、ajax 获取后端数据(jquery):跨域问题

(1)下载jQuery

(2)使用jQuery发送ajax

<template>
<div id="app"> <!-- 2.3 使用Vheader组件 -->
<Vheader></Vheader> <router-view/> <!-- 路由切换 -->
</div>
</template> <script>
// 1.0 导入bootstrap
import "bootstrap/dist/css/bootstrap.min.css" // 2.1 先引入vheader
import Vheader from "./components/Vheader" // 3.0 引入jQuery
import $ from 'jquery' export default {
name: 'App',
data(){
return {
// allList:[] 不会在data存储太多的数据
}
}, // 2.2 挂载Vheader
components:{
Vheader
},
created(){ },
mounted(){ // 3.1 ajax请求数据
var _this = this; $.ajax({
url:'http://127.0.0.1:9527/api/v1/comments/',
methods:"get",
success:function(data){
console.log(data)
console.log(_this) _this.$store.state.allList = data;
} })
}
}
</script> <style> </style>

data

(3)vuex: this 如何定位allList

(4) 跨域问题

django后端settings后端设置

https://www.jianshu.com/p/3961366f9ce9

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'app01',
] MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', # 注意顺序
]
#跨域增加忽略
CORS_ORIGIN_ALLOW_ALL = True #允许所有源访问(如果不需要允许全部,可以设置CORS_ORIGIN_WHITELIST=()参数,将需要访问的域名添加即可) CORS_ALLOW_CREDENTIALS = True #是否允许携带cookie CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
) CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)

5、allList of state of store 如何使用?

(1)VnoteList.vue

<template>
<ul>
<VnoteItem v-for="(item,index) in getAllDatas" :data="item"></VnoteItem>
</ul> </template> <script>
import VnoteItem from "./VnoteItem"
export default {
name:"VnoteList",
data(){
return{}
},
components:{
VnoteItem,
},
computed:{
// 监听
getAllDatas(){
return this.$store.state.allList;
}
}
}
</script> <style scoped> </style>

(2)VnoteItem.vue

..

<template>
<li >
<h2>{{ data.title }}</h2>
<p>{{ data.content }}</p>
</li> </template> <script>
export default {
name:"VnoteItem",
data(){
return{ }
},
props:{
// 验证
data:Object
} }
</script> <style scoped> </style>

2、ajax提交数据

post提交一条笔记

1、效果

2、流程如何走

(1)APP.vue + main.js

显示页面+控制vuex的store数据

   

(2)Vnote显示Vmark组件

3、Vmark组件

<template>
<div class='wrap'>
请输入文字标题: <input type="text" name="" v-model="titleHandler"> <button class="btn btn-success" @click="addOneNote">提交</button> <div class="mark"> <textarea rows="10" cols="100" class='editor' v-model='markdownHandler' ></textarea> <div class='show' v-html='currentValue' ref="t"></div> </div>
</div>
</template> <script> // 导入jQuery
import $ from "jquery" import Marked from "marked";
export default {
name: "Vmark",
data() {
return {
// markValue: ""
};
},
methods: {
// 添加1条笔记
addOneNote(){
console.log(this.$refs.t)
console.log(this.$refs.t.innerText) var json = {
title:this.titleHandler, // 1.3 标题添加到json
markdown:this.markdownHandler,
content:this.$refs.t.innerText, }
console.log(json); // 每次提交刷新数据
var _this = this; // ajax请求 post
$.ajax({
url:"http://127.0.0.1:9527/api/v1/comments/",
method:"post",
data:json,
success:function(data){
console.log(data)
_this.$store.state.allList = data // allList笔记列表,更新数据
},
error:function(err){
console.log(err)
}
})
}
},
computed: {
// 1.0 监听标题
titleHandler:{
set:function(newValue){ // 1.2 设置标题的值,给store中的note
console.log(newValue)
this.$store.state.note.title = newValue
},
get:function(){ //1.1 获取标题的值
return this.$store.state.note.title
}
}, // 监听markdown
markdownHandler:{
set:function(newValue){
console.log(newValue)
this.$store.state.note.markdown = newValue
},
get:function(){
return this.$store.state.note.markdown
}
}, currentValue() {
return Marked(this.$store.state.note.markdown);
}
}
};
</script> <style> .mark {
width: 800px;
height: 400px;
margin: 0 auto;
}
.editor,.show {
float: left;
width: 395px;
height: 400px;
border: 1px solid #666;
}
</style>

(1) Vmark监听数据

(2)computed计算属性:$store.state.note

(3)click提交数据+ajax提交json+刷新数据

(4)markdown格式:获取一块标签 refs.t

3、mutations 如何使用(不推荐)

Mutaitons官网 https://vuex.vuejs.org/zh/guide/mutations.html

1、改变store中的store

2、$ajax: get方法

原冗余代码

main.js 声明回调函数

App.vue 调用 回调函数

3、$ajax:post方法

原冗余ajax

main.js 声明回调函数,带参数

Vmark.vue调用回调函数 ,传参数

4、Action(推荐)

https://vuex.vuejs.org/zh/guide/actions.html

1、为什么使用Action?异步

2、如何使用

ation --->commit触发---->mutations

dispatch分发Action

5、Element

1、介绍

前端UI对比:https://www.cnblogs.com/elesos/p/9533204.html

ElementUI https://element.eleme.cn/#/zh-CN/component/installation

2、使用element

在 main.js 中写入以下内容:

import Vue from 'vue';
import App from './App.vue'; // 使用element
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); new Vue({
el: '#app',
render: h => h(App)
});

使用btn按钮

3、封装自己的按钮(slot)

(1)达到这种效果

(2)class去计算属性

(3)实现

VnoteItem.vue 声明,引入按钮

<template>
<li >
<h2>{{ data.title }}</h2>
<p>{{ data.content }}</p>
<!-- <el-button type="success" round>成功按钮</el-button> -->
<VnoteBtn typed='info' >删除按钮</VnoteBtn> </li> </template> <script>
import VnoteBtn from "./VnoteBtn"
export default {
name:"VnoteItem",
data(){
return{ }
},
props:{
// 验证
data:Object
},
computed:{ },
methods:{
// var id // dispatch
},
components:{
VnoteBtn
} }
</script> <style scoped> </style>

VnoteBtn.vue组件,设计

<template>
<button class="btn" :class = "currentBtn" >
<!-- 插槽 分发 -->
<slot>按钮</slot> </button>
</template> <script>
export default {
name:"VnoteBtn",
data(){
return { }
},
props:{
typed:String
},
computed:{
currentBtn(){
console.log(this.typed)
return{
"btn-success": this.typed == "success"? true:false,
"btn-info": this.typed == "info"? true:false,
}
}
} }
</script> <style scoped> </style>

4、后续作业

删除一条数据

点击一条数据,赋值到markdown

6、axios技术:vue中的ajax

处理请求

之前用jquery的$.ajax

vue也有,axios技术

axios文档 https://www.kancloud.cn/yunye/axios/234845

axios中文官网 http://www.axios-js.com/zh-cn/docs/

06-vue项目02:vuex、Mutation、Action、ElementUI、axios的更多相关文章

  1. vue项目配置vuex

    在vue项目中各组件之间传值非常的好用,但是当组件数量多的时候,就会感觉到多个组件之间传值就会变的非常痛苦.因此就需要使用vuex来管理数据值,这样在任何页面不需要传值过来的情况下就可以拿到我们想要的 ...

  2. 前端Vue项目——首页/课程页面开发及Axios请求

    一.首页轮播图 1.elementUI走马灯 elementUI中 Carousel 走马灯,可以在有限空间内,循环播放同一类型的图片.文字等内容. 这里使用指示器样式,可以将指示器的显示位置设置在容 ...

  3. vue项目中解决跨域问题axios和

    项目如果是用脚手架搭建的(vue cli)项目配置文件里有个proxyTable proxyTable是vue-cli搭建webpack脚手架中的一个微型代理服务器,配置如下 配置和安装axios 安 ...

  4. Vue项目中引入ElementUI

    前提:创建好的vue项目. 1.安装ElementUI 转到项目根目录,输入命令:#cnpm install element-ui --save-dev 2.在 main.js 引入并注册 impor ...

  5. Vue学习日记(四)——Vue状态管理vuex

    前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...

  6. vue用npm安装删除模块element-ui mint-ui

    vue用npm安装删除模块element-ui mint-ui 在vue项目中先引入了element-ui,后来发现移动版的需要用mint-ui,所以需要先卸载了再安装.卸载element-ui:np ...

  7. vue项目的架构设计完善详解

    vue项目构建vuex+mock层 vue项目添加jsBridge(与原生交互的) vue项目添加代码格式化

  8. 在vue项目中,解决如何在element表格中循环出图片列!

    效果图: 1,vue项目环境 2,引入element-ui组件 3,制作表格 此处省去制作循环表格数据那步,想看的可以找回我的博客:element中的表格处理:循环出表格数据 今天想在表格出循环出一列 ...

  9. vue项目--favicon设置以及动态修改favicon

    最近写公司项目时,动态更新favicon 动态更新之前需要有一个默认的favicon. 目前vue-cli搭建的vue项目里面已经有了一个static文件夹,存放静态文件. favicon图片放到该文 ...

  10. 【vue】vue +element 搭建项目,vuex中的store使用

    概述: 每一个 Vuex 应用的核心就是 store(仓库).“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state).Vuex 和单纯的全局对象有以下两点不同: Vuex 的 ...

随机推荐

  1. lua数据类型的的操作(三)

    上一章我们学习了lua的数据类型,以及语法的定义,今天我们学习lua的数据类型操作,其实就是lua库一些api的操作,遇到对数据类型处理时,可以根据lua库提供的操作来实现. 一.字符串操作 1.字符 ...

  2. c# base64及MD5工具类

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  3. 初识php语法

    初到一家php公司,由于之前做的java,现在记录一些学习php中的语法细节. =>的用法 => 是数组成员访问符号.在php中数组默认键名是整数,也可以自己定义任意字符键名(最好是有实际 ...

  4. Selenium工具爬取商品

    selenium是一个优秀的自动化测试工具,支持多种语言,具体介绍参考官方文档:https://www.seleniumhq.org/docs/. 下面我们使用selenium工具模拟用户点击商品详情 ...

  5. 1.CentOS 7 vs CentOS 6的不同

    CentOS 7 vs CentOS 6的不同(1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell)   (2)文件系统[CentOS6] ...

  6. analysis_tools

  7. fastclick插件学习(一)之用法

    原理 在检测到touchend事件后, 会通过dom自定义事件模拟一个click事件,并把浏览器300ms之后真正触发的点击事件屏蔽掉,fastclick是不会对PC浏览器添加监听事件 使用 1.引入 ...

  8. EF Core的级联删除

    级联删除由DeleteBehavior的枚举值来设置: 行为名称 对内存中的依赖项/子项的影响 对数据库中的依赖项/子项的影响 Cascade 删除实体 删除实体 ClientSetNull 外键属性 ...

  9. 判断两个list是否元素一样

    首先创建枚举 public enum TheType { type1 = , type2 = , type3 = } 1.如果不考虑顺序,即顺序不一样,只要元素都一样即可 List<TheTyp ...

  10. 二元变量图形的pandas方法

    数据加载: 1.散点图 上图使用下采样的方法选取了100个样本点,因为把所有的数据加载进来太多了. 2.Hexplot图 上图是一个散点图再加上热力标注的形式,可以更准确的帮助我们看出数据集中在哪些区 ...