<h2 id="全局配置">全局配置
用法
Vue.config.silent=true
取消Vue所有的日志与警告
// 务必在加载 Vue 之后,立即同步设置以下内容 Vue.config.devtools = true
Vue.config.errorHandler = function (err,vm,info) { // handle error // `info` 是 Vue 特定的错误信息,比如错误所在的生命周期钩子 // 只在 2.2.0+ 可用 }
Vue.config.warnHandler = function (msg,trace) { // `trace` 是组件的继承关系追踪 }
为 Vue 的运行时警告赋予一个自定义处理函数。注意这只会在开发者环境下生效,在生产环境下它会被忽略。
Vue.config.ignoredElements = [ 'my-custom-web-component','another-web-component',// 用一个 `RegExp` 忽略所有“ion-”开头的元素 // 仅在 2.5+ 支持 /^ion-/ ]
Vue.config.keyCodes = { v: 86,f1: 112,// camelCase 不可用 mediaPlayPause: 179,// 取而代之的是 kebab-case 且用双引号括起来 "media-play-pause": 179,up: [38,87] } <input type="text" @keyup.media-play-pause="method">
<input type="text" @keyup.media-play-pause="method">
给 v-on 自定义键位别名。
全局API并不在构造器里,而是先声明全局变量或者直接在Vue上定义一些新功能,Vue内置了一些全局API。说的简单些就是,在构造器外部用Vue提供给我们的API函数来定义新的功能。
data选项是特例,需要注意在Vue.extend()中它必须是函数
// 创建构造器 var Profile = Vue.extend({ template: '{{firstName}} {{lastName}} aka {{alias}}',data: function () { return { firstName: 'Walter',lastName: 'White',alias: 'Heisenberg' } } }) // 创建 Profile 实例,并挂载到一个元素上。 new Profile().$mount('#mount-point') 结果如下: Walter White aka Heisenberg 参数: {function} [callback] {Object} [context] 用法: 在下次DOM更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的DOM // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 Vue.nextTick(function () { // DOM 更新了 }) // 作为一个 Promise 使用 (2.1.0 起新增,详见接下来的提示) Vue.nextTick() .then(function () { // DOM 更新了 }) 2.1.0 起新增:如果没有提供回调且在支持 Promise 的环境中,则返回一个 Promise。请注意 Vue 不自带 Promise 的 polyfill,所以如果你的目标浏览器不原生支持 Promise (IE:你们都看我干嘛),你得自己提供 polyfill。 Vue.set的作用就是在构造器外部操作构造器内部的数据,属性或者方法。比如在vue构造器内部定义一个count为1的数据,我们构造器外部定义了一个方法,要每次点击按钮给值加1,就需要用到Vue.set. 参数 {Object | Array} target {string | number} key {any} value 返回值:设置的值 用法 设置对象的属性,如果对象是响应式的,确保属性被创建后也是响应式的,同时触发视图更新。这个方法主要用于避开Vue不能检测属性被添加的限制 1、用Vue.set改变 function add(){ Vue.set(outData,'count',4); } 2、用Vue对象的方法添加 app.count++; 3、直接操作外部数据 outData.count++; 由于javascript的限制,Vue不能自动检测以下变动的数组 当你利用索引值直接设置一个项时,Vue不会为我们自动更新 当你修改数组的长度时,Vue不会为我们自动更新 参数 {Object | Array} target {string | number} key/index 用法 删除对象的属性。如果对象是响应式的,确保删除能触发更新视图。这个方法主要用于避开Vue不能检测到属性被删除的限制,但是你应该很少会使用它。 参数 {string} id {Function | Object} [definition] 用法: 注册或获取全局指令 js // 注册 Vue.directive('my-directive',{ bind: function () {},inserted: function () {},update: function () {},componentUpdated: function () {},unbind: function () {} }) // 注册 (指令函数) Vue.directive('my-directive',function () { // 这里将会被 bind 和 update 调用 }) // getter,返回已注册的指令 var myDirective = Vue.directive('my-directive') <h3 id="vue.filteriddefinition">Vue.filter(id,[definition]) 参数: {string} id {Function} [definition] 用法: 注册或获取全局过滤器 / 注册 Vue.filter('my-filter',function (value) { // 返回处理后的值 }) // getter,返回已注册的过滤器 var myFilter = Vue.filter('my-filter') 参数 {string} id {Function | Object} [definition] 用法 注册或获取全局组件。注册还会自动使用给定的id设置组建的名称 / 注册组件,传入一个扩展过的构造器 Vue.component('my-component',Vue.extend({ /* ... */ })) // 注册组件,传入一个选项对象 (自动调用 Vue.extend) Vue.component('my-component',{ / ... / }) // 获取注册的组件 (始终返回构造器) var MyComponent = Vue.component('my-component') 局部注册组件和全局注册组件是相对应的,局部注册的组件只能在组件注册的作用域里进行使用,其他作用域使用无效 component组件 全局组件 局部注册组件 component组件的props属性设置 参数:{Object | Function} plugin 用法: 安装vue.js插件。如果插件是一个对象,必须提供install方法。如果插件是一个函数,它会被作为install方法。install方法调用时,会将Vue作为参数传入。 当install方法被同一个插件多次调用,插件将只会被安装一次。 参数 {string} template 用法:在render函数中编译模板字符串。只在独立构建时有效 var res = Vue.compile('{{ msg }}') new Vue({ data: { msg: 'hello' },render: res.render,staticRenderFns: res.staticRenderFns }) 直接在构造器里的template选项后边编写。这种写法比较直观,但是模板html代码太多,不建议这么写 var app=new Vue({ el:'#app',data:{ message:'hello Vue!' },template:` ` }) 标签里的模板 这种方法更像是在写html代码,就算不会写vue的人,也可以制作页面 <script type="text/javascript"> var app=new Vue({ el:'#app',data:{ message:'hello Vue!' },template:'#demo2' }) </script></code></pre> <script type="text/javascript"> var app=new Vue({ el:'#app',template:'#demo3' }) </script></code></pre> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
{{firstName}} {{lastName}} aka {{alias}}
结果如下:
Walter White aka Heisenberg
在下次DOM更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的DOM
// 修改数据 vm.msg = 'Hello' // DOM 还没有更新 Vue.nextTick(function () { // DOM 更新了 }) // 作为一个 Promise 使用 (2.1.0 起新增,详见接下来的提示) Vue.nextTick() .then(function () { // DOM 更新了 })
// 作为一个 Promise 使用 (2.1.0 起新增,详见接下来的提示) Vue.nextTick() .then(function () { // DOM 更新了 })
2.1.0 起新增:如果没有提供回调且在支持 Promise 的环境中,则返回一个 Promise。请注意 Vue 不自带 Promise 的 polyfill,所以如果你的目标浏览器不原生支持 Promise (IE:你们都看我干嘛),你得自己提供 polyfill。
{any} value
设置对象的属性,如果对象是响应式的,确保属性被创建后也是响应式的,同时触发视图更新。这个方法主要用于避开Vue不能检测属性被添加的限制
1、用Vue.set改变
function add(){ Vue.set(outData,'count',4); }
2、用Vue对象的方法添加 app.count++;
3、直接操作外部数据
outData.count++;
{string | number} key/index
删除对象的属性。如果对象是响应式的,确保删除能触发更新视图。这个方法主要用于避开Vue不能检测到属性被删除的限制,但是你应该很少会使用它。
js
// 注册 Vue.directive('my-directive',{ bind: function () {},inserted: function () {},update: function () {},componentUpdated: function () {},unbind: function () {} }) // 注册 (指令函数) Vue.directive('my-directive',function () { // 这里将会被 bind 和 update 调用 }) // getter,返回已注册的指令 var myDirective = Vue.directive('my-directive')
// 注册 (指令函数) Vue.directive('my-directive',function () { // 这里将会被 bind 和 update 调用 })
bind
update
// getter,返回已注册的指令 var myDirective = Vue.directive('my-directive')
{Function} [definition]
注册或获取全局过滤器
/ 注册 Vue.filter('my-filter',function (value) { // 返回处理后的值 })
// getter,返回已注册的过滤器 var myFilter = Vue.filter('my-filter')
{Function | Object} [definition]
注册或获取全局组件。注册还会自动使用给定的id设置组建的名称
/ 注册组件,传入一个扩展过的构造器 Vue.component('my-component',Vue.extend({ /* ... */ })) // 注册组件,传入一个选项对象 (自动调用 Vue.extend) Vue.component('my-component',{ / ... / }) // 获取注册的组件 (始终返回构造器) var MyComponent = Vue.component('my-component')
// 注册组件,传入一个选项对象 (自动调用 Vue.extend) Vue.component('my-component',{ / ... / })
// 获取注册的组件 (始终返回构造器) var MyComponent = Vue.component('my-component')
局部注册组件和全局注册组件是相对应的,局部注册的组件只能在组件注册的作用域里进行使用,其他作用域使用无效
component组件 全局组件 局部注册组件
全局组件
局部注册组件
component组件的props属性设置
用法:在render函数中编译模板字符串。只在独立构建时有效
var res = Vue.compile('{{ msg }}') new Vue({ data: { msg: 'hello' },render: res.render,staticRenderFns: res.staticRenderFns })
new Vue({ data: { msg: 'hello' },render: res.render,staticRenderFns: res.staticRenderFns })
直接在构造器里的template选项后边编写。这种写法比较直观,但是模板html代码太多,不建议这么写
var app=new Vue({ el:'#app',data:{ message:'hello Vue!' },template:` ` })
这种方法更像是在写html代码,就算不会写vue的人,也可以制作页面
<script type="text/javascript"> var app=new Vue({ el:'#app',data:{ message:'hello Vue!' },template:'#demo2' }) </script></code></pre>
<script type="text/javascript"> var app=new Vue({ el:'#app',template:'#demo3' }) </script></code></pre>
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!