注意:以下所有示例基于vue 2.x、Vuex 2.x、
vm.$mount()-挂载
组件示例(component):
a. 不使用template标签
{{ m }}
点这里-> a:{{a}} b:{{b}}
c. 局部注册
bbb
d. 动态组件
bbb
e. 父子组件通信
父:
子:
f. 兄弟组件通信
计算示例(computed):
{{ intro }}
自定义指令实现"点击按钮使文本框获取焦点"示例(directive):
简单的todolist:
剩余{{ remain }}条未完成
{{ key.a }}---{{ key.b }}
×
使用jquery调用接口数据:
{{ list }}
使用vue-resource调用接口数据:(Vue2推荐使用axios代替vue-resource)
加载中...
{{ list }}
匿名slot1
匿名slot2
具名slot1
bbb
ccc
activate示例:
vuex示例:
a. 简单计数
{{ num }}
b. 子组件获取Vuex状态
父:{{ num }}
c. 拓展状态Getters
转换后:{{ num }},长度:{{ len }},点击换一换
Time Travel状态正常{{ num }}
Time Travel状态不正常{{ num }}
e. actions中使用Promise
{{ num }}
f. 模块Modules和根节点状态rootState
{{ num }}
directive示例:(请注意和vue示例的数据交互使用的是
vnode.context,
待更新)
export default {
data() {
return {
SC_outer_style: {
},
SC_inner_style: {
height: '50px',
background: 'red'
}
}
},
computed: {
SC_outer_style() {
return {
}
}
},
mounted() {
},
methods: {
init() {
}
},
directives: {
handle: {
inserted: function(el, binding, vnode) {
if(Tool.getStyle(el, 'position') == 'static') {
vnode.context.SC_outer_style = {
position: 'relative'
}
}
},
}
}
}
vue-router示例:
a. 简单的单页应用
b.
嵌套路由
home-content
new
old
new-content
old-content
about-content
b. 路由进阶
index
home-content(点击输出 route 对象)
route 对象:{{ $route | json }}
new
old
new-content
old-content
about-content
编写插件示例:(配合es6语法,待更新)
a. 自调用
myPlugin.js:
import Vue from 'vue';
;(function () {
var MyPlugin = {};
MyPlugin.install = function (Vue, options) {
Vue.directive('mySex', {
inserted(el, binding) {
console.log(binding.value)
}
})
Vue.prototype.$sex = options.sex;
Vue.prototype.$say = function() {
console.log(this.$sex)
};
};
Vue.use(MyPlugin, {sex: 'male'});// 这里调用后,引用该文件无需再调用
})();
main.js:
import Vue from 'vue';
import MyPlugin from 'js/myPlugin.js';
b. export default
myPlugin.js:
import Vue from 'vue';
export default {
install(Vue, options) {
Vue.directive('mySex', {
inserted(el, binding) {
console.log(binding.value)
}
})
Vue.prototype.$sex = 'female';
Vue.prototype.$say = function() {
console.log(this.$sex)
};
}
}
main.js:
import Vue from 'vue';
import MyPlugin from 'js/myPlugin.js';
Vue.use(MyPlugin, {sex: 'male'});// 这里需要调用一次
c. 自定义组件(也是一种插件方式)
myPlugin.vue:
{{ sex }}
main.js:
import Vue from 'vue';
import MyPlugin from 'components/myPlugin.vue';
new Vue({
el: '#app',
components: {
'my-plugin': MyPlugin
}
})
index.html:
demo