var vm = new Vue({
//选项
})
<div id="div_test">
<h1>hello : {{first}}</h1>//veu使用{{valuename}}来绑定数据和函数
<h1>world : {{second}}</h1>
<h1>{{details()}}</h1>
</div>
var vm = new Vue({
el: '#div_test',//元素选择器,可以是#class,.id,元素名等等
data: {//定义的属性,属性按照键值对形式定义
first: "hello",
second: "world",
third: "this my first vue!"
},
methods: {//定义的函数
details: function(){
return this.third;
}
}
})
var data = vm.$data;//使用$来调用vue示例的属性和方法
<div id="app">
<p v-if="check">1</p>
<p v-else>2</p>
<button v-on:click="check = !check">change</button>
</div>
<script>
new Vue({
el: "#app",
data: {
check: false
},
})
</script>
<div id="app">
<ul>
<li v-for="(number, index) in numbers">
{{ number }}
{{ index }}
</li>
</ul>
</div>
<script>
new Vue({
el: "#app",
data: {
numbers: [1, 2, 3]
},
})
</script>
<div id="app">
<div v-for="(number, index) in numbers">
<p>{{ number }}</p>
<p>{{ index }}</p>
</div>
</div>
<script>
new Vue({
el: "#app",
data: {
numbers: [1, 2, 3]
},
})
</script>
可以使用
<div id="app">
<template v-for="(number, index) in numbers">
<p>{{ number }}</p>
<p>{{ index }}</p>
</template>
</div>
<div id = "div_test">
<p>{{s}}</p>
<p>{{reverseMessage}}</p>
</div>
var vm = new Vue({
el: "#div_test",
data: {
s: "abcd"
},
computed: {//因为是属性,函数插入的时候不用带(),而methods需要。绑定属性的时候需要带()
reverseMessage: function(){
return this.s.split("").reverse().join("");
}
}
})
var vm = new Vue({
el: "#someid",
data: {
message: "someMessage"
},
computed: {
someFunction: {
get: function(){//函数插入默认会调用的方法
return this.message;
},
set: function(value){
this.message = value;
}
}
}
});
vm.someFunction = "otherMessage";//使用set
<div id="div_test">
<p>{{count}}</p>
<input type="button" @click="count++" value="click me">
</div>
第一种:
var vm = new Vue({
el: "#div_test",
data: {
count: 1
},
watch: {
count: function(val){//属性监听会在属性变化后立刻执行,然后页面中的{{count}}才会变化,变量val是变化后的属性值
console.log("count chang to %d", val)
}
//count: '方法名'
//count: {//选项对象
// handler: function(){ ... },
// immediate: true,//是否初始化时立刻执行
// deep: true//是否深入监听每一个元素
//}
}
})
还可以这样使用:
var vm = new Vue({
el: "#div_test",
data: {
count: 1
}
});
vm.$watch("count", function(oldv, newv){//变量oldv、newv分别是变化前的属性值和变化的属性值
console.log("the count from %d change to %d", oldv, newv);
});
<style>
.dmeo{
height: 100px;
width: 100px;
background: blue;
}
</style>
<div id="div_test" v-bind:class="{'demo': active}">当demo为true时将样式demo绑定到div,当然可以传入多个class</div>
var vm = new Vue({
el: "div_test",
data: {
active: true
}
})
<div id="div_test" v-bind:style="divStyle">
样式也可以写为v-bind: style="{fontSize: data属性+'px'}"的形式,也可以传入数组[style1, style2]来绑定多个样式
</div>
var vm = new Vue({
el: "div_test",
data: {
divStyle: {
fontSize: 100 + "px"
}
}
})
<div id="app">
<p v-bind:class="{ blue:check, 'red':!check}" v-on:click="check=!check">132</p>
</div>
<script>
new Vue({
el: "#app",
data: {
check: true
}
})
</script>
<div id="app">
<input type="text" v-mode="name">
{{ name }}
</div>
<script>
new Vue({
el: "#app",
data: {
name: ""
}
})
</script>
<div id="app">
<input type="text" ref="test" v-on:keyup="logName">
{{ name }}
</div>
<script>
new Vue({
el: "#app",
data: {
name: ""
},
methods: {
logName: function (event) {
this.name = this.$refs.test.value;
}
}
})
</script>
Vue.component("my-component", {
data: function(){
return {
key: value
}
},
template: "that is a component!
"
})
<div id="div_test">
<my-demo></my-demo>
</div>
var comp = {
template: "hello world!
"//template模型只能有一个根标签
};
//在全局环境注册组件
Vue.component("my-demo", comp);
//新建实例使用组件
new Vue({
el: "#div_test"
})
var comp = {
template: "hello world!
"
};
//单个实例下注册的组件
new Vue({
el: "#div_test",
components: {
"my-demo": comp
}
})
var vm = new Vue({
el: "#div_test",
compoents: {my-zuJian1, my-zuJian2},
template: "";//表示要选择的组件为my-zuJian1
})
<script type="x-template" id="templateDemo">
<p>模板</p>
</script>
<script>
var vm = new Vue({
el: "#div_test",
template: "#templateDemo";//表示要选择的组件为id为templateDemo的模板(template中文翻译是模板)
})
</script>
<div id="app">
<my-component message="hello"></my-component>
</div>
Vue.component("my-component", {
props: ["message"],
//标准写法应该是:
//props: {
// message: {
// type: String,
// require: true
// }
//}
//这样写更加严谨
template: " {{ message }}
"
});
new Vue({
el: "#app"
})
<div id="app" v-bind:style="{fontSize: size + 'px'}">
<!--父级组件可以在使用子组件的地方直接使用 v-on 来监听子组件触发的事件,相当于子组件向父组件传递数据-->
<!--因为vue有自己的事件系统,所以在父级组件中假如要使用原生的事件,则需要使用.natuve修饰符,因为vue期望的是一个自定义事件-->
<my-component v-bind:message="hello" v-on:bigger="test" v-bind:style="{fontSize: size + 'px'}"></my-component>
{{ size }}
</div>
Vue.component("my-component", {
props: ["message"],
template: ""
});
new Vue({
el: "#app",
data: {
size: 15
},
methods: {
test: function () {
console.log(this.size++)
}
}
});
<div id="app">
<my-component v-bind:num="123" v-bind:bool="ad"></my-component>
</div>
<script>
Vue.component("my-component", {
props: {
num: Number,
bool: Boolean
},
template: " {{ num }}
{{ bool }}
"
});
new Vue({
el: "#app"
})
</script>
上面的 props 在将值传递给子组件的时候会对值的类型进行检验,假如符合则传递,不符合会报 undefined 错误(有例外,给Boolean传数字也会通过检验)<div id="id-demo">
<test>world</test>
</div>
<script>
Vue.component('test',{
template: `hello
`,
});
new Vue({
el: '#id-demo',
data: {
},
methods: {
}
})
</script>
可以包含的内容包括所有模板代码、html、字符串、其它组件等等,假如组件模板里面没有 slot,则使用组件的时候,被组件标签包含的内容都会被抛弃
<div id="id-demo">
<test>
<template v-slot:section1>world</template>
<template v-slot:section2>!</template>
</test>
</div>
<script>
Vue.component('test',{
template: `hello
`,
});
new Vue({
el: '#id-demo'
})
</script>
不带 name 的卡槽默认名字是 default,可以对 default 进行显示命名控制其中的内容;任何没有被带有 v-slot 包裹的内容都会被当做默认内容
<div id="id-demo">
<test>
<!--v-slot:section="sectionProp" 意味着可以使用sectionProp来访问名字为section的卡槽的所有绑定的属性-->
<template v-slot:section="sectionProp">{{ sectionProp.user.sex }}</template>
</test>
</div>
<script>
Vue.component('test',{
template: `hello {{ user.name }}
`,
data: function () {
return {
user: {
name: 'jame',
sex: 'man'
}
}
}
});
new Vue({
el: '#id-demo',
})
</script>
首先将子组件的数据绑定到卡槽的属性上(比如上面的代码中,slot 将数据 user 绑定到自己的属性 user 上),然后父组件使用 v-slot:卡槽名=“自定义卡槽上的绑定属性名” 来使用
<div id="id-demo">
<component :is="'test-' + check"></component>
<button @click="check = !check">click</button>
</div>
<script>
Vue.component('test-true',{
template: `hello
`,
});
Vue.component('test-false',{
template: `world
`,
});
new Vue({
el: '#id-demo',
data: {
check: true
}
})
</script>
{
path: ':id',
name: 'Ticket',
component: Ticket,
props: route => { return { id: route.params.id } }//适合 props 是动态时使用
},
{
path: ':id',
name: 'Ticket',
component: Ticket,
props: { id: 'abc' }//适合 props 是动态时使用
}
//定义全局指令
Vue.directive("commandName", {
钩子函数: function(el, binding, vnode, oldVnode){
.....
}
})
//定义局部指令
new Vue({
el: "#app"
directive: {
commandName: {
钩子函数: function(el, binding, vnode, oldVnode){
.....
}
}
}
...
})
cnpm install vue-router --save-dev
import vueRouter from 'vue-router'
Vue.use(veuRouter);
const router = new vueRouter({
routes: [
{ path: '/XXX', name: 'XXX', component: 'XXX' },
//children用于定义子路由,redirect用于定义子路由的默认页面
{ path: '/XXX', name: 'XXX', component: 'XXX', redirect: 'ZZZ', children: [{二级路由}, {}] },
{ path: '*', redirect: YYY }//默认路由地址
],
mode: "history"//去掉每次切换路径的时候,url会带上#/
});
new Vue({
...
router,
...
})
<router-link to="/path">使用路由path</router-link>
<router-link :to="{ name: 'XXX' }">使用路由name,也可以是组件自定义data</router-link>
<router-link to="/path" tag="div">使用div标签</router-link>
methods: {
back: function () {
this.$router.go(-1);//返回上次路由
},
go: function () {
this.$router.replace('routerPath');//通过路径返回指定路由
//this.$router.replace({ name: 'routerName' });//通过路由名字返回指定路由
//push()的效果和replace()差不多,不同的地方是replace()将浏览器中的当前条目替换为新路由,而不添加新的条目
//this.$router.push('routerPath')
//this.$router.push({ name: 'routerName' })
}
}
new Router({
routers: [
{
path: '/login/:userName',//动态路由的可变部分以“:”标示
name: 'Login',
component: Login
}
]
})
可以使用 $route.params.userName 来获取到变化的部分router.beforeEach((to, from, next) => {
alert(1);//每次路由改变都会弹出1
});
//在组件的template中使用的 router-view 使用一个name属性
<template>
<div id="app">
<router-view name="meanue"></router-view>
</div>
</template>
//然后在路由表里面指定组件
new Router({
routers: [
{ path: '/', name: 'Home', components: { default: Home, 'meanue': Meanue } }
]
})
cnpm install vue-resource --save
created: function () {
this.$http.get("http://jsonplaceholder.typicode.com/users").then((data) => {
console.log(data);
})
}
//全局过滤器
Vue('filterName', func)
{{ data | filterName }}
<div id="id-demo">
<ul v-for="num in nums">
<li>{{ num | check }}</li>
</ul>
</div>
<script>
Vue.filter('check', function (num) {
if (num > 0)
return num;
else
return -1;
});
new Vue({
el: '#id-demo',
data: {
nums: [5, 6, -8, -2]
}
})
</script>
mode: 'history',
//scrollBehavior函数期望返回值是一个可以有两种不同形式的对象:
//{ x: 100, y: 100 }
//{ selector: '#foo', offset: { x: 0, y: 200 } }
scrollBehavior(to, from, savedPosition){
if (savedPosition){//savedPosition是浏览器历史记录中每个条目自动保存的位置
return savedPosition
}
if (to.hash){
return{ selector: to.hash }
}
return{ x: 0, y: 0 }
}