触发视图更新:css
push() pop() shift() unshift() splice() sort() reverse()
因为 JavaScript 的限制,Vue 不能检测如下变更的数组:vue
因为 JavaScript 的限制,Vue 不能检测对象属性的添加或删除:webpack
在开发过程当中,咱们时常会遇到这样一种状况:当vue的data里边声明或者已经赋值过的对象或者数组(数组里边的值是对象)时,向对象中添加新的属性,若是更新此属性的值,是不会更新视图的。web
根据官方文档定义:若是在实例建立以后添加新的属性到实例上,它不会触发视图更新。
例如:数组
this.obj = Object.assign({}, this.obj, { a: 1, e: 2 })
.lazy
在默认状况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 (除了上述输入法组合文字时)。你能够添加 lazy 修饰符,从而转变为使用 change 事件进行同步:函数
<!-- 在“change”时而非“input”时更新 --> <input v-model.lazy="msg" >
filter中获取不到this
解决方法:性能
var vm = new Vue({ data () { return { content: 'abcd' } }, filters: { XXX: function(data){ return vm.content; } }, ... });
<template> <div>{{ content | reverseStr(content) }}</div> </template> new Vue({ data () { return { content: 'abcd' } }, filters: { XXX: function(data, content){ return content; } }, ... });
@hook
能够监听到子组件的生命周期钩子函数(created
,updated
等等).
例如:@hook:mounted="doSomething"
ui
<template> <Child v-bind="$props" v-on="$listeners" @hook:mounted="doSomething"> </Child> </template> <script> import Child from "./Child"; export default { props: { title: { required: true, type: String } } components: { Child }, methods: { doSomething(){ console.log("child component has mounted!"); } } }; </script>
函数式组件, 无状态,没法实例化,内部没有任何生命周期处理方法,很是轻量,于是渲染性能高,特别适合用来只依赖外部数据传递而变化的组件。this
子组件:code
<template functional> <div> <p v-for="(name, idx) in props.users" @click="props.clickHandler(name)" :key="idx"> {{ name }} </p> </div> </template>
父组件:
<template> <div> <UserList :users="users" :click-handler="clickHandler.bind(this)"></UserList> </div> </template> <script> import UserList from "./UserList"; export default { name: "App", data: () => { users: ['james', 'ian'] } components: { UserList }, methods: { clickHandler(name){ console.log(`clicked: ${name}`); } } }; </script>
第一步:CSS Modules 必须经过向css-loader
传入modules: true
来开启
第二步:在<style>
上添加module
特性
// webpack.config.js { module: { rules: [ { test: /\.css$/, use: [ 'vue-style-loader', { loader: 'css-loader', options: { // 开启 CSS Modules modules: true, // 自定义生成的类名 localIdentName: '[local]_[hash:base64:8]' } } ] } ] } } <style module> .red { color: red; } .bold { font-weight: bold; } </style>
这个 module 特性指引 Vue Loader 做为名为 $style 的计算属性,向组件注入 CSS Modules 局部对象。而后你就能够在模板中经过一个动态类绑定来使用它了:
<template> <p :class="$style.red"> This should be red </p> </template>
CSS 属性名能够用驼峰式 (camelCase) 或短横线分隔 (kebab-case,记得用引号括起来) 来命名:
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div> 或 data: { activeColor: 'red', fontSize: 30 }