Vuex
定义:Vuex是Vue配套的公共数据管理工具,我们可以将共享的数据保存到Vuex中,方便整个程序中的任何组件都可以获取和修改Vuex中保存的公共数据
state
爷孙三代使用vuex轻松获取同一数据,不需要靠传递 1.引入vuex
<script src="js/vuex.js" type="text/javascript" charset="utf-8"></script>
2.创建一个Vuex对象
const store = new Vuex.Store({
//这里的state相当于Vue中的data
state:{
name :"yxy"
}
})
3.将vuex对象传递到Vue实例中
//将vuex对象传递到Vue实例中
store: store,
4.调用state数据
<!-- 调用state数据 -->
<p>{{this.$store.state.name}}</p>
实操代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="demo">
<grandfather></grandfather>
</div>
<template id="grandfather">
<div id="">
<p>{{this.$store.state.name}}</p>
<father></father>
</div>
</template>
<template id="father">
<div id="">
<p>这是父组件{{this.$store.state.name}}</p>
<son></son>
</div>
</template>
<template id="son">
<p>这是子组件{{this.$store.state.name}}</p>
</template>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="js/vuex.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
const store = new Vuex.Store({
state:{
name :"yxy"
}
})
Vue.component("grandfather", {
template: "#grandfather",
components: {
"father": {
template: "#father",
components: {
"son": {
template: "#son"
}
}
}
}
})
var app = new Vue({
el: "#demo",
store: store,
data: {
},
methods: {
},
directives: {
},
components: {
}
})
</script>
</body>
</html>
修改共享数据
子孙三代都能修改组件共享数据
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="demo">
<grandfather></grandfather>
</div>
<template id="grandfather">
<div id="">
<button type="button" @click="add">增加</button>
<button type="button" @click="sub">减少</button>
<input type="" name="" id="" :value="this.$store.state.count"/>
<father></father>
</div>
</template>
<template id="father">
<div id="">
<button type="button" @click="add">增加</button>
<button type="button" @click="sub">减少</button>
<input type="" name="" id="" :value="this.$store.state.count"/>
<son></son>
</div>
</template>
<template id="son">
<div id="">
<button type="button" @click="add">增加</button>
<button type="button" @click="sub">减少</button>
<input type="" name="" id="" :value="this.$store.state.count"/>
</div>
</template>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="js/vuex.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
const store = new Vuex.Store({
state:{
count :0
},
mutations: {
madd(state){
state.count++;
},
msub(state){
state.count--;
}
}
})
Vue.component("grandfather", {
template: "#grandfather",
store: store,
methods:{
add(){
this.$store.commit("madd")
},
sub(){
this.$store.commit("msub")
}
},
components: {
"father": {
template: "#father",
methods:{
add(){
this.$store.commit("madd")
},
sub(){
this.$store.commit("msub")
}
},
components: {
"son": {
template: "#son",
methods:{
add(){
this.$store.commit("madd")
},
sub(){
this.$store.commit("msub")
}
},
}
}
}
}
})
var app = new Vue({
el: "#demo",
data: {
},
methods: {
},
directives: {
},
components: {
}
})
</script>
</body>
</html>
getters属性
组件中的computed和vuex中的getters都是类似Vue中的计算属性,如果结算结果不发生改变,那么computed和getters中的函数都只会执行一次 实操代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="demo">
</div>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="js/vuex.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
const store = new Vuex.Store({
state: {
},
getters:{
}
})
var app = new Vue({
el: "#demo",
data: {
},
methods: {
},
directives: {
},
components: {
}
})
</script>
</body>
</html>
Vue Router
Vue Router和v-show、v-if一样,都是用来切换组件的显示的 先导入Vue Router 1.定义组件
const one = {
template: "#one"
}
const two = {
template: "#two"
}
2.定义路由规则
const routers = [
{path:"/one", component: one},
{path:"/two", component: two}
]
3.根据自定义的路由规则创建路由对象Vue Router
const router = new VueRouter({
routes:routers
})
4.将创建好的路由对象Vue Router绑定到Vue实例中
var app = new Vue({
el: "#demo",
router:router,
data: {
},
methods: {
},
directives: {
},
components: {
one: one,
two: two
}
})
5.修改哈希值
<a href="#/one">跳转到第一个页面</a>
<a href="#/two">跳转到第二个页面</a>
6.给Vue实例组件中添加router-view标签渲染Vue Router
<router-view></router-view>
router-link
可以在前一板块中的第五步中替换a标签
<router-link to="/one" tag="button">第一个页面</router-link>
<router-link to="/two" tag="button">第二个页面</router-link>
定义路由规则时添加以下代码,表示默认情况下第二个叶念为点击状态
{
path: "/",
redirect: "/two"
},
创建路由对象时:
const router = new VueRouter({
routes: routers,
linkActiveClass: "active"
})
router传递参数
传递参数的两种办法 通过url参数的方式来进行传递参数: 1.在指定哈希的时候,通过?key=value&key=value的方式来进行传递
<router-link to="/one?name=zs&age=18" tag="button">第一个页面</router-link>
2.在传递的组件的生命周期的方法中通过this.$route.query.key的方式来获取参数
const one = {
template: "#one",
created:function(){
console.log(this.$route.query);
console.log(this.$route.query.name);
console.log(this.$route.query.age);
}
}
通过路由规则中的占位符来进行参数传递 1.在路由规则中先指定一个/:key/:key
const routers = [
{
path: "/",
redirect: "/two"
},
{
path: "/one",
component: one
},
{
path: "/two/:name/:age",
component: two
}
]
2.在指定哈希时,通过/value/value的方式传递key中的数据
<router-link to="/two/ls/20" tag="button">第二个页面</router-link>
3.在传递的组件的生命周期的方法中通过this.$route.params.key的方式来获取参数
const two = {
template: "#two",
created:function(){
console.log(this.$route.params.name)
console.log(this.$route.params.age)
}
}
嵌套路由
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
}
.tem1 {
width: 300px;
height: 500px;
background-color: antiquewhite;
}
.oneChildren1 {
width: 200px;
height: 400px;
background-color: blue;
}
.oneChildren2 {
width: 200px;
height: 400px;
background-color: green;
}
.tem2 {
width: 300px;
height: 500px;
background-color: aqua;
}
.active {
background-color: aquamarine;
}
</style>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="demo">
<router-link to="/one" tag="button">第一个页面</router-link>
<router-link to="/two" tag="button">第二个页面</router-link>
<router-view></router-view>
</div>
<template id="one">
<div id="" class="tem1">
<h1>这是第一个页面</h1>
<router-link to="/one/onebut1" tag="button">第一个页面</router-link>
<router-link to="/one/onebut2" tag="button">第二个页面</router-link>
<router-view></router-view>
</div>
</template>
<template id="onebut1">
<div id="" class="oneChildren1">
<h1>这是第一个子界面页面</h1>
</div>
</template>
<template id="onebut2">
<div id="" class="oneChildren2">
<h1>这是第二个子界面页面</h1>
</div>
</template>
<template id="two">
<div id="" class="tem2">
<h1>这是第二个页面</h1>
</div>
</template>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="js/vueRouter.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
const onebut1 = {
template: "#onebut1",
}
const onebut2 = {
template: "#onebut2",
}
const one = {
template: "#one",
components: {
onebut1: onebut1,
onebut2: onebut2
}
}
const two = {
template: "#two",
}
const routers = [{
path: "/",
redirect: "/one",
},
{
path: "/one",
component: one,
children: [{
path: "onebut1",
component: onebut1
},
{
path: "onebut2",
component: onebut2
},
],
},
{
path: "/two",
component: two
}
]
const router = new VueRouter({
routes: routers,
linkActiveClass: "active"
})
var app = new Vue({
el: "#demo",
router: router,
data: {
},
methods: {
},
directives: {
},
components: {
one: one,
two: two,
onebut1: onebut1,
onebut2: onebut2
}
})
</script>
</body>
</html>
watch属性
1.可以监听数据的变化并且启动对应的回调函数
var app = new Vue({
el: "#demo",
data: {
num1:"0",
num2:"0",
sum:"0"
},
watch: {
num1:function(newData, oldData){
this.sum = parseInt(this.num1) + parseInt(this.num2);
},
num2:function(newData, oldData){
this.sum = parseInt(this.num1) + parseInt(this.num2);
}
},
methods: {
},
directives: {
},
components: {
}
})
2.可以监听路由地址 通过在watch中添加路由路由地址,再产生一个回调函数,就可以得到监听的路由地址
Vue的生命周期
先来看一下官方给出的图片 
Vue的生命周期=>创建阶段
具体看注释
var app = new Vue({
el: "#demo",
beforeCreate:function(){
},
created:function(){
},
beforeMount:function(){
},
mounted:function(){
console.log(document.getElementsByTagName("p")[0].innerHTML);
console.log(document.getElementsByTagName("p")[0].innerText);
},
data: {
name:"yxy"
},
methods: {
say(){
console.log("hello");
}
},
directives: {
},
components: {
}
})
Vue的生命周期=>运行阶段
beforeUpdate:function(){
},
updated:function(){
console.log(this.name)
console.log(document.getElementsByTagName("p")[0].innerHTML)
},
Vue的生命周期=>销毁阶段
通过组件展示
Vue.component("temp", {
template:"#temp1",
data:function(){
return {
msg:"yes"
}
},
methods:{
say(){
console.log("hello");
}
},
beforeDestroy:function(){
},
destroyed:function(){
}
})
Vue中获取DOM元素的方法(ref)
<div id="demo">
<button type="button" @click="myFn">获取DOM元素</button>
<p ref="dom1">我是p标签</p>
<temp ref="domC"></temp>
</div>
<template id="temp1">
<p>我是组件1</p>
</template>
methods: {
myFn(){
console.log(this.$refs);
console.log(this.$refs.dom1);
console.log(this.$refs.domC);
console.log(this.$refs.domC.msg);
console.log(this.$refs.domC.say);
}
},
|