过滤器
过滤器使用filter. 过滤器是表示格式化数据的
过滤器分为全局过滤器和局部过滤器
过滤器的使用:
过滤器可以在插值语法中使用,也可以在v-bind表达式(属性绑定)中使用
| 是管道符号
<div id="app">不传前缀符号的局部过滤:{{money | preSymbol()}}不传前缀符号的局部过滤:{{money | preSymbol}}<br/>传前缀符号的局部过滤:{{money | preSymbol('$')}}<br/>全局过滤:{{money | preSymbolG()}}<br/>全局过滤11:{{money | preSymbolG('$')}}<div :title="money | preSymbolG('$')">在v-bind中使用</div></div>// 全局过滤器(|符号的前一个值传入money)Vue.filter('preSymbolG', (money, symbol = "¥") => {return symbol + money;})new Vue({el: '#app',data: {money: 1234},// 局部过滤器filters: {// (|符号的前一个值传入money)preSymbol (money, symbol = "¥") {return symbol + money;}}})
自定义指令
自定义指令使用directive.
自定义指令一般是封装对DOM元素进行底层操作的方法,并且这个方法是需要复用的
更多的钩子函数需要在官方文档查看
<div id="app"><!-- 无参数的自定义指令 --><input type="text" v-model="inp" v-focus><!-- 有参数的自定义指令 自定义指令后面是表达式 --><button v-admin="{rooter: 'other'}">删除管理员</button></div>// 自定义指令Vue.directive('focus', {inserted (el) {el.focus();}})// 通过自定义指令来操作元素Vue.directive('admin', {inserted (el, bindings) {console.log(bindings)console.log(bindings.value.rooter)if(bindings.value.rooter !== 'admin') {el.parentElement.removeChild(el);}}})new Vue({el: '#app',data: {inp: ''}})
渲染函数
作用:当程序更新的时候再次执行,得到最新的虚拟DOM
渲染函数使用renderrender函数传入createdElement
<script src="https://at.alicdn.com/t/font_2341569_n1zksf3dnza.js"></script><style>.iconfont {font-family: "iconfont" !important;font-size: 16px;font-style: normal;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;}.icon {width: 1em;height: 1em;vertical-align: -0.15em;fill: currentColor;overflow: hidden;}</style><div id="app"><h1 :title="title"><svg class="icon"><use xlink:href="#icon-shop"></use></svg>{{title}}</h1><heading leave="1" title="12331" icon="shop">{{title}}</heading></div>Vue.component('heading', {props: {leave: {type: String,default: '1'},title: String,icon: String},render (h) {// 因为生成虚拟DOM的源码就是调用h的方法let children = [];if (this.icon) {const icon = h('svg', {attrs: {class: 'icon'},},[h('use', {attrs: {'xlink:href': `#icon-${this.icon}`}})])children.push(icon);}children = children.concat(this.$slots.default)console.log(this.$slots.default, children)return h('h'+this.leave,{attrs: {title: this.title}},children)}})new Vue({el: '#app',data: {title: '购物车'}})
虚拟DOM是真实DOM的映射,它是描述真实DOM的方式,它更轻量,快速
查看官方文档
函数式组件
如果组件没有监听器,没有data(没有响应式数据),没有生命周期,则可以使用函数式组件
函数式组件没有实例
函数式组件的使用没有响应式数据了,提高了性能(更加轻量,消耗资源更少)
查看官方文档
<heading leave="1" title="12331" icon="shop">{{title}}</heading>// 函数式组件Vue.component('heading', {functional: true,props: {leave: {type: String,default: '1'},title: String,icon: String},render (h, context) {// 因为生成虚拟DOM的源码就是调用h的方法let children = [];let {icon, leave, title } = context.props;if (icon) {const iconVNode = h('svg', {attrs: {class: 'icon'},},[h('use', {attrs: {'xlink:href': `#icon-${icon}`}})])children.push(iconVNode);}children = children.concat(context.children)console.log(context.children, children)return h('h'+leave,{attrs: {title}},children)}})new Vue({el: '#app',data: {title: '购物车'}})
混入
混入使用mixin
混入是一种设计模式
混入式组件扩展,逻辑复用的一种方式
选项合并规则在官方文档中查看(插件开发和源码中会使用)
<heading leave="1" title="12331" icon="shop">{{title}}</heading>// 混入中是一个对象const myMixin = {created() {this.hello();},methods: {hello () {console.log('hello')}}}Vue.component('heading', {props: {leave: {type: String,default: '1'},title: String,icon: String},// 混入,可以混入多个。如果组件内定义的函数名和数据名和混入定义的重复,则以组件中的为准mixins: [myMixin],render (h) {// 因为生成虚拟DOM的源码就是调用h的方法let children = [];if (this.icon) {const icon = h('svg', {attrs: {class: 'icon'},},[h('use', {attrs: {'xlink:href': `#icon-${this.icon}`}})])children.push(icon);}children = children.concat(this.$slots.default)console.log(this.$slots.default, children)return h('h'+this.leave,{attrs: {title: this.title}},children)}})new Vue({el: '#app',data: {title: '购物车'}})
插件
插件时vue扩展的终极方案
前面的几种方案不适合分发,即分享给别人
如果需要写vue的插件,则需要实现install方法
插件的使用Vue.use(插件)
Vue可以添加静态方法,申明指令或过滤器,或者混入,在混入中可以拿到组件实例,添加原型方法
MyHeadering.js(插件)const MyHeadering = {install (Vue, options) {Vue.component('heading', {props: {leave: {type: String,default: '1'},title: String,icon: String},render (h) {// 因为生成虚拟DOM的源码就是调用h的方法let children = [];if (this.icon) {const icon = h('svg', {attrs: {class: 'icon'},},[h('use', {attrs: {'xlink:href': `#icon-${this.icon}`}})])children.push(icon);}children = children.concat(this.$slots.default)console.log(this.$slots.default, children)return h('h'+this.leave,{attrs: {title: this.title}},children)}})}}if(window !== undefined && window.Vue) {// 插件的使用window.Vue.use(MyHeadering)}插件的使用<div id="app"><heading leave="1" title="12331" icon="shop">{{title}}</heading></div><script src="../vue.js"></script><script src="plugin/headering.js"></script><script>new Vue({el: '#app',data: {title: '购物车'}})</script>
