1、vue介绍html
Vue.js 是一套构建用户界面(UI)的渐进式JavaScript框架,是一个轻量级MVVM(model-view-viewModel)框架。vue
2、数据绑定node
Mustache(插值语法)
,也就是 {{}}
语法{{}}
从数据对象data
中获取数据{{}}
中只能出现JavaScript表达式 而不能解析js语句
双向数据绑定:将DOM与Vue实例的data数据绑定到一块儿,彼此之间相互影响ios
原理:Object.defineProperty
中的get
和set
方法ajax
getter
和setter
:访问器读取或设置
对象属性值的时候,执行的操做3、vue指令npm
v-text、v-html、v-if、v-show、v-for、v-bind(绑定标签属性)、v-on(绑定事件)、v-model(绑定表单)json
v-on
在监听键盘事件时添加关键修饰符,eg:@keyup.enter="add"
4、过滤器filtersaxios
1.全局过滤器浏览器
1 <div>{{ dateStr | date }}</div> 2 <div>{{ dateStr | date('YYYY-MM-DD hh:mm:ss') }}</div> 3 4 <script> 5 Vue.filter('date', function(value, format) { 6 // value 要过滤的字符串内容,好比:dateStr 7 // format 过滤器的参数,好比:'YYYY-MM-DD hh:mm:ss' 8 }) 9 </script>
2.局部过滤器服务器
1 { 2 data: {}, 3 // 经过 filters 属性建立局部过滤器 4 // 注意:此处为 filters 5 filters: { 6 filterName: function(value, format) {} 7 } 8 }
5、监听数据变化watch
watch
是一个对象,键是须要观察的表达式,值是对应回调函数
1 new Vue({ 2 data: { a: 1, b: { age: 10 } }, 3 watch: { 4 a: function(val, oldVal) { 5 // val 表示当前值 6 // oldVal 表示旧值 7 console.log('当前值为:' + val, '旧值为:' + oldVal) 8 }, 9 10 // 监听对象属性的变化 11 b: { 12 handler: function (val, oldVal) { /* ... */ }, 13 // deep : true表示是否监听对象内部属性值的变化 14 deep: true 15 }, 16 17 // 只监视user对象中age属性的变化 18 'user.age': function (val, oldVal) { 19 }, 20 } 21 })
6、计算属性computed
1 var vm = new Vue({ 2 el: '#app', 3 data: { 4 firstname: 'jack', 5 lastname: 'rose' 6 }, 7 computed: { 8 fullname() { 9 return this.firstname + '.' + this.lastname 10 }//注意不能和data里面重名 11 } 12 })
7、vue生命周期
8、axios
npm i -S axios
一、建立axios文件
1 import axios from "axios"; 2 import qs from "qs"; 3 import { Indicator, Toast } from "mint-ui"; 4 5 const Axios = axios.create({ 6 baseURL: "",//接口域名 7 timeout: 5000, 8 responseType: "json", 9 // withCredentials: true, // 是否容许带cookie这些 10 headers: { 11 "Content-Type": "application/x-www-form-urlencoded;charset=utf-8" 12 } 13 }); 14 15 Axios.interceptors.request.use( // POST传参序列化(添加请求拦截器) 16 config => { 17 // 在发送请求以前作某件事 18 if ( 19 config.method === "post" || 20 config.method === "put" || 21 config.method === "delete" 22 ) { 23 // 序列化 24 config.data = qs.stringify(config.data); 25 } 26 27 // 如果有作鉴权token , 就给头部带上token 28 // if (localStorage.token) { 29 // config.headers.Authorization = localStorage.token; 30 // } 31 32 Indicator.open({ 33 // text: '加载中...', 34 spinnerType: 'fading-circle' 35 });//mint-ui加载动画 36 37 return config; 38 }, 39 error => { 40 console.log(error) 41 Toast({ 42 message: error, 43 position: 'middle', 44 duration: -1 45 }); 46 Indicator.close(); 47 return Promise.reject(error); 48 } 49 ); 50 51 Axios.interceptors.response.use( // 响应拦截器 52 response => { 53 Indicator.close(); 54 // if (res.status != 200) { 55 // alert(res.statusText); 56 // return Promise.reject(res); 57 // } 58 if (response.data == null && response.config.responseType === 'json' && response.request.responseText != null) { 59 try { 60 // eslint-disable-next-line no-param-reassign 61 response.data = JSON.parse(response.request.responseText); 62 } catch (e) { 63 // ignored 64 } 65 } 66 return response; 67 }, 68 error => { 69 Indicator.close(); 70 if (error.response) { 71 // 请求已发出,但服务器响应的状态码不在 2xx 范围内 72 73 } else { 74 //一些错误是在设置请求的时候触发 75 76 console.log('Error', error.message); 77 } 78 79 Toast({ 80 message: error.message, 81 position: 'middle', 82 duration: -1 83 }); 84 85 return Promise.reject(error); 86 } 87 ); 88 89 90 export default { 91 install: function (Vue, Option) { 92 Object.defineProperty(Vue.prototype, "$axios", { value: Axios }); 93 } 94 };
---
// 在组件中使用: methods: { getData() { this.$axios.get('url') .then(res => {}) .catch(err => {}) } }
9、组件之间通信
父组件到子组件(经过props)
props
属性显示指定,不然,不会生效props
属性的用法与data
属性的用法相同1 <div id="app"> 2 <!-- 若是须要往子组件总传递父组件data中的数据 须要加v-bind="数据名称" --> 3 <hello v-bind:msg="info"></hello> 4 <!-- 若是传递的是字面量 那么直接写--> 5 <hello my-msg="abc"></hello> 6 </div> 7 8 <!-- js --> 9 <script> 10 new Vue({ 11 el: "#app", 12 data : { 13 info : 15 14 }, 15 components: { 16 hello: { 17 // 建立props及其传递过来的属性 18 props: ['msg', 'myMsg'], 19 template: '<h1>这是 hello 组件,这是消息:{{msg}} --- {{myMsg}}</h1>' 20 } 21 } 22 }) 23 </script>
子组件到父组件
$emit()
触发自定义事件事件 this.$emit(pfn,参数列表。。。)1 <hello @pfn="parentFn"></hello> 2 3 <script> 4 Vue.component('hello', { 5 template: '<button @click="fn">按钮</button>', 6 methods: { 7 // 子组件:经过$emit调用 8 fn() { 9 this.$emit('pfn', '这是子组件传递给父组件的数据') 10 } 11 } 12 }) 13 new Vue({ 14 methods: { 15 // 父组件:提供方法 16 parentFn(data) { 17 console.log('父组件:', data) 18 } 19 } 20 }) 21 </script>
非父子组件通信
1 import Vue from 'Vue' 2 3 export const Event = new Vue() // 注册单一事件管理组件通讯
1 <!-- 组件A: --> 2 <com-a></com-a> 3 <!-- 组件B: --> 4 <com-b></com-b> 5 6 <script> 7 import Event from '../assets/js/eventBus' 8 // 通讯组件 9 var vm = new Vue({ 10 el: '#app', 11 components: { 12 comB: { 13 template: '<p>组件A告诉我:{{msg}}</p>', 14 data() { 15 return { 16 msg: '' 17 } 18 }, 19 created() { 20 // 给中间组件绑定自定义事件 注意:若是用到this 须要用箭头函数 21 bus.$on('tellComB', (msg) => { 22 this.msg = msg 23 }) 24 } 25 }, 26 comA: { 27 template: '<button @click="emitFn">告诉B</button>', 28 methods: { 29 emitFn() { 30 // 触发中间组件中的自定义事件 31 bus.$emit('tellComB', '土豆土豆我是南瓜') 32 } 33 } 34 } 35 } 36 }) 37 </script>
10、内容分发
11、获取组件(或元素) - refs
vm.$refs
一个对象,持有已注册过 ref 的全部子组件(或HTML元素)ref
属性,而后在JS中经过vm.$refs.属性
来获取1 <div id="app"> 2 <div ref="dv"></div> 3 <my res="my"></my> 4 </div> 5 6 <!-- js --> 7 <script> 8 new Vue({ 9 el : "#app", 10 mounted() { 11 this.$refs.dv //获取到元素 12 this.$refs.my //获取到组件 13 }, 14 components : { 15 my : { 16 template: `<a>sss</a>` 17 } 18 } 19 }) 20 </script>