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. HashMap集合-遍历方法

    # HashMap集合-遍历方法 先定义好集合: public static void main(String[] args) { Map<String,String> onemap=ne ...

  2. 什么是DataV数据可视化

    DataV数据可视化是使用可视化大屏的方式来分析并展示庞杂数据的产品.DataV旨让更多的人看到数据可视化的魅力,帮助非专业的工程师通过图形化的界面轻松搭建专业水准的可视化应用,满足您会议展览.业务监 ...

  3. springboot问题

    1.导入数据库jar包后,配置好,发现报错 数据库连接不成功  加上@SpringBootApplication(exclude = DataSourceAutoConfiguration.class ...

  4. Python http.server中获取Post的请求报文

    今天在自学http.server请求命令, 各个字段都很好理解, 但唯独想打印获取Post请求报文时, 被难住了, 网上找了很多帖子, 官方的文档也刷了几遍, 但没有一个明确的答复. 后来不经意间看到 ...

  5. 贝叶斯优化 Bayesian Optimization

    贝叶斯优化 Bayesian Optimization 2018年07月02日 22:28:06 余生最年轻 阅读数 4821更多 分类专栏: 机器学习   版权声明:本文为博主原创文章,遵循CC 4 ...

  6. where用法

    where 子句用于指定类型约束. 1.接口约束 public class MyGenericClass<T> where T:IComparable { }  2.基类约束: 指出某个类 ...

  7. python selenium3 模拟点击+拖动+保存验证码 测试对象 58同城验证码

    #!/usr/bin/python # -*- coding: UTF-8 -*- # @Time : 2019/12/5 17:30 # @Author : shenghao/10347899@qq ...

  8. Java Web-EL表达式 in JSP

    Java Web-EL表达式 in JSP 概念 EL(Expression Language)是一种表达式语言,可以替换和简化JSP页面上JAVA代码的书写 语法 ${<在这里写表达式> ...

  9. js重点——作用域——作用域分类(三)

    一.作用域可以分为全局作用域,局部作用域(函数作用域)和块级作用域. 1.全局作用域 代码在程序中的任何位置都能被访问到,window对象的内置属性都拥有全局作用域. <script> v ...

  10. LCD1602 液晶

    它是一种专门用来显示字母.数字.符号的点阵型液晶模块 它是由若干个5x7或者5x10的点阵字符位组成 引脚 时序 操作步骤 初始化 写命令(RS=L)设置显示坐标 写数据(RS=H) 指令 清屏: 指 ...