VUE2.0学习css
核心库
只关注视图层,可是vue并不仅关注视图,和angular同样也有指令,过滤器这些东西# 全局安装 vue-cli $ npm install --global vue-cli # 建立一个基于 webpack 模板的新项目 $ vue init webpack my-project # 安装依赖,走你 $ cd my-project $ npm install $ npm run dev
npm install vue-loader -save-dev
{ test: /\.js$/, // 用正则来匹配文件路径,这段意思是匹配 js 或者 jsx loader: 'babel'// 加载模块 "babel" 是 "babel-loader" 的缩写 }, { test: /\.vue$/, loader: 'vue' }
vue: { loaders: { js: 'babel' } }
{ "presets": ["es2015","stage-0","stage-1","stage-2","stage-3"] // "plugins": ["transform-runtime"] }
resolve: { extensions: ['', '.js', '.json', '.scss', '.vue'] }
npm install vue -save
npm install vue-template-compiler -save
import Vue from 'vue' import AppContainer from '../containers/AppContainer' const app = new Vue({ render: h => h(AppContainer), }).$mount('#app') // new Vue({ // el:'#app', // render: h => h(App) // })
<template> <div> {{msg}} </div> </template> <style> body{ background-color:#ff0000; } </style> <script> export default{ data(){ return{ msg:'hello vue' } } } </script>
实例.动态属性名
实例.$实例属性名
获取this.a
去获取动态属性this.$data
去获取实例属性var data = {a: 1} const app = new Vue({ // 和数据相关的都挂载到data上 data: data, // 和方法相关的都挂载到methods上 methods: { // this和$的使用 func1: function () { console.log(this.a); console.log(this.$data.a); }, changeData:function () { this.a=2 } } }) // 先监听再改变 app.$watch('a', function (newVal, oldVal) { console.log(newVal) console.log(oldVal) }) // 值改变以后会自动执行监听方法 // data的值是能够直接改变的,和react的setState方法不同,由于vue里面用了ES6的set和get方法,能够起到自动监听的做用 app.a=3 // 动态属性和实例属性 // console.log(app) // console.log(app.a) // console.log(app.$data.a)
// 错误的写法 <div id="{{id}}"></div> // 正确的写法 <div v-bind:id="id"></div>
{{ number + 1 }} {{ ok ? 'YES' : 'NO' }} {{ message.split('').reverse().join('') }} <div v-bind:id="'list-' + id"></div>
{{ message | capitalize }} {{ message | filterA | filterB }} new Vue({ // ... filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } } })
<a v-on:click="doSomething">
<form v-on:submit.prevent="onSubmit"></form>
<!-- 完整语法 --> <a v-bind:href="url"></a> <!-- 缩写 --> <a :href="url"></a>
<!-- 完整语法 --> <a v-on:click="doSomething"></a> <!-- 缩写 --> <a @click="doSomething"></a>
计算属性 vs methodshtml
<template> <div> <p>Original message: "{{ message }}{{name}}"</p> <p>Computed reversed message: "{{ reversedMessage }}"</p> <p>Computed reversed message: "{{ reverseMessage() }}"</p> </div> </template> <style> body{ background-color:#ff0000; } </style> <script> export default{ data(){ return{ message: 'Hello', name:'a' } }, mounted(){ this.name="b" }, computed: { reversedMessage: function () { console.log('计算属性被调用了') return this.message.split('').reverse().join('') } }, methods: { reverseMessage: function () { console.log('方法被执行了') return this.message.split('').reverse().join('') } } } </script>
计算属性 vs watchvue
<!--页面--> <div id="demo">{{ fullName }}</div>
//watch写法 var vm = new Vue({ el: '#demo', data: { firstName: 'Foo', lastName: 'Bar', fullName: 'Foo Bar' }, watch: { firstName: function (val) { this.fullName = val + ' ' + this.lastName }, lastName: function (val) { this.fullName = this.firstName + ' ' + val } } }) //计算属性的写法 //本质是你要获取全名 var vm = new Vue({ el: '#demo', data: { firstName: 'Foo', lastName: 'Bar' }, computed: { fullName: function () { return this.firstName + ' ' + this.lastName } } })
计算setterreact
// 接上面的代码段 computed: { fullName: { // getter get: function () { return this.firstName + ' ' + this.lastName }, // setter set: function (newValue) { var names = newValue.split(' ') this.firstName = names[0] this.lastName = names[names.length - 1] } } }
<template>
标签包裹,v-if做用在template标签上<template>
语法<my-components is="todo-item" v-for="(item, index) in todos" v-bind:title="item" v-on:remove="item.splice(index, 1)" > </my-components>
// <li v-for="n in evenNumbers">{{ n }}</li> data: { numbers: [ 1, 2, 3, 4, 5 ] }, computed: { evenNumbers: function () { return this.numbers.filter(function (number) { return number % 2 === 0 }) } }
<button v-on:click="warn('Form cannot be submitted yet.', $event)">Submit</button>
methods: { warn: function (message, event) { // 如今咱们能够访问原生事件对象 if (event) event.preventDefault() alert(message) } }
<!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a> <!-- 提交事件再也不重载页面 --> <form v-on:submit.prevent="onSubmit"></form> <!-- 修饰符能够串联 --> <a v-on:click.stop.prevent="doThat"></a> <!-- 只有修饰符 --> <form v-on:submit.prevent></form> <!-- 添加事件侦听器时使用时间捕获模式 --> <div v-on:click.capture="doThis">...</div> <!-- 只当事件在该元素自己(而不是子元素)触发时触发回调 --> <div v-on:click.self="doThat">...</div> <!-- the click event will be triggered at most once --> <a v-on:click.once="doThis"></a>
<!-- 只有在 keyCode 是 13 时调用 vm.submit() --> <input v-on:keyup.13="submit"> <!-- 同上 --> <input v-on:keyup.enter="submit"> <!-- 缩写语法 --> <input @keyup.enter="submit"> <!--所有的按键别名:--> enter tab delete (捕获 “删除” 和 “退格” 键) esc space up down left right ctrl alt shift meta
// 可使用 v-on:keyup.f1 Vue.config.keyCodes.f1 = 112
<textarea></textarea>
并不会生效,应用v-model来代替<input type="radio" v-model="pick" v-bind:value="a">
<!-- 在 "change" 而不是 "input" 事件中更新 --> <input v-model.lazy="msg" >
Vue.component('shiguoqing', { template: '<div>石国庆</div>' }) const app = new Vue({ el:'#example' })
<template> <div> 姓名:石国庆,年龄:{{age}},性别:{{genderSex}} </div> </template> <style scope> div{ background-color:orange; } </style> <script> export default{ data(){ return{ } }, props:['age','genderSex'] } </script>
<template> <div> <shi-guoqing></shi-guoqing> {{msg}} </div> </template> <style> body{ background-color:#ff0000; } </style> <script> import Shiguoqing from '../components/Shiguoqing.vue' export default{ data(){ return{ msg:'hello vue' } }, components:{ 'shi-guoqing':Shiguoqing } } </script>
当你在一些特殊标签如table,ul,ol,select中使用自定义组件的时候会有一些限制webpack
<table> <my-row>...</my-row> </table>
<table> <tr is="my-row"></tr> </table>
应当注意,若是您使用来自如下来源之一的字符串模板,这些限制将不适用:git
<script type="text/x-template">
☆在自定义组件中data属性必须是函数形式☆github
<!-- 传递实际的数字 --> <comp v-bind:some-prop="1"></comp>
☆注意在JavaScript中对象和数组是引用类型,指向同一个内存空间,若是prop是一个对象或数组,在子组件内部改变它会影响父组件的状态。web
咱们在传递属性的时候能够作属性校验vue-router
Vue.component('currency-input', { template: '\ <span>\ $\ <input\ ref="input"\ v-bind:value="value"\ v-on:input="updateValue($event.target.value)"\ >\ </span>\ ', props: ['value'], methods: { // 不是直接更新值,而是使用此方法来对输入值进行格式化和位数限制 updateValue: function (value) { var formattedValue = value // 删除两侧的空格符 .trim() // 保留 2 小数位 .slice(0, value.indexOf('.') + 3) // 若是值不统一,手动覆盖以保持一致 if (formattedValue !== value) { this.$refs.input.value = formattedValue } // 经过 input 事件发出数值 this.$emit('input', Number(formattedValue)) } } })
// 在根组件中注册bus属性 const app = new Vue({ data:{ bus:new Vue() }, render: h => h(AppContainer), }).$mount('#app')
// 在子组件中使用 this.$root.bus.$emit('eventName',2323)
做用域插槽是一种特殊类型的插槽,用做使用一个(可以传递数据到)可重用模板替换已渲染元素。vuex
组件的递归调用
<unique-name-of-my-component name="unique-name-of-my-component"></unique-name-of-my-component>
组件的循环引用
一、本博客中的文章摘自网上的众多博客,仅做为本身知识的补充和整理,并分享给其余须要的coder,不会用于商用。
二、由于不少博客的地址看完没有及时作保存,因此不少不会在这里标明出处,很是感谢各位大牛的分享,也但愿你们理解。