Vue父组件向子组件传递数据
1 | <div id="app"> |
这里子组件里的:init-count指的是props中的initCount,其对应的值是count(父组件中的count),指定是Number类型。
Vue的is特性
包括自定义元素和特殊标签,如1
2
3
4
5
6
7
8
9
10
11
12
13<div id="app">
<table>
<tbody is="my-component"></tbody>
</table>
</div>
<script>
Vue.component('my-component', {
template: '<div>Table</div>'
});
new Vue({
el: '#app'
})
</script>
使用v-model进行组件通讯
子组件接受一个value属性,在有新的value时触发input事件,在子组件接受父组件的方法时,使用watch来监听接收的value的变化。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42<div id="app">
{{ value }}
<my-com v-model="value"></my-com>
<button @click="handleMinus">-1</button>
</div>
<script>
Vue.component('my-com', {
props: {
value: {
type: Number
}
},
template: '<div>{{ currentValue }}<button @click="handleClick">+1</button></div>',
data () {
return {
currentValue: this.value
}
},
watch: {
value (val) {
this.currentValue = val;
}
},
methods: {
handleClick () {
this.currentValue ++;
this.$emit('input', this.currentValue);
}
}
});
new Vue({
el: '#app',
data: {
value: 1
},
methods: {
handleMinus () {
this.value --;
}
}
});
</script>
slot用法
1 | <div id="app"> |
原本app的message为world,我们需要在子组件中使用slot标签显示父组件中的message,当app中没有内容的时候,就会显示slot标签中的默认内容。
多个slot用法:使用具名方法
访问slot:在vue2.0中,使用$slots来访问,单个slot时default为默认,使用具名时则填写对应的具名。
内联模板
1 | <com1 inline-template> |
当组件使用了inline-template后,内容 将不再是 slot ,而是这个 com1 组件的 template ,也就是会渲染出内容。
手动挂载
1 | <div id="app"> |
使用$.mount()手动挂载到div中
vue-router技巧
1 | const router = new Router({ |
1.这里设置了history模式,官方上给出的介绍是:vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。如果不想要很丑的 hash,我们可以用路由的 history 模式,这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面。
2.设置path:’*’,没有匹配到的路由,将跳转到’/index’中
3.使用router.beforeEach改变其网页的title,使用next();进入到下一个钩子当中
4.使用router.afterEach让跳转后的回到页面顶部
5.component里的default:默认路由写法对应视图中同时有两个
新版vue-cli本地服务器加载本地资源
1.在webpack.dev.conf.js文件1
2
3
4
5
6```
const express = require('express')
const app = express()
var appData = require('../mock/goods.json')
var apiRoutes = express.Router()
app.use('/api', apiRoutes)
2.在1
2
3
4
5
6```
before(apiRoutes) {
apiRoutes.get('/api/goods', function(req, res, next) {
res.json(appData)
})
}
使用过滤器以及计算属性
1 | {{ text | filterA }} |
通过Vue实例添加选项filters来设置:1
2
3
4
5filter: {
filterA: function (value) {
return value++ //返回过滤后的数据
}
}
计算属性:1
{{ reversedText }}
在Vue实例computed选项中:1
2
3
4
5computed: {
reversedText: function () {
return this.text.split(',').reverse().join(',') //返回data内text属性修改后的内容
}
}
(简书笔记搬运 写于2017.06.29 13:45)