vue­router路由和前端状态 管理

vue­router路由基本加载

四步走
1.安装

npm install --save vue-router
  1. 引用
import router from 'vue-router'
Vue.use(router

3.配置路由文件,并在vue实例中注入

var rt = new router({
routes:[{
path:'/',//指定要跳转的路径
component:HelloWorld//指定要跳转的组件
}]
})
new Vue({
el: '#app',
router:router,
components: { App },
template: '<App/>'
})

  1. 确定视图加载的位置
<router-view></router-view>

通过vue-router路由的跳转

1.配置路由

import router from 'vue-router'
import Vue from "vue"
import HelloWorld from '../components/HelloWorld'
import HelloEarth from '../components/HelloEarth'



Vue.use(router)
//配置路由
export default new router({
  routes: [{
    path: '/helloworld', //指定要跳转的路径
    component: HelloWorld //指定要跳转的组件
  }, {
    path: '/helloearth', //指定要跳转的路径
    component: HelloEarth //指定要跳转的组件
  }]
})

2.设置router-link

<template>
  <ul>
    <li>
      <router-link to="/helloworld">Hello World</router-link>
    </li>
    <li>
      <router-link to="/helloearth">Hello Earth</router-link>
    </li>
  </ul>
</template>

<script>
export default {
  name: "list"
};
</script>

<style>
</style>

3.在app中添加list路由

vue-router参数的传递

1.必须在路由内加入路由的name
2.必须在path后加/: +传递的参数

export default new router({
  routes: [{
    name: 'helloworld',
    path: '/helloworld/:worldmsg', //指定要跳转的路径
    component: HelloWorld //指定要跳转的组件
  }, {
    name: 'helloearth',
    path: '/helloearth/:earthmsg', //指定要跳转的路径
    component: HelloEarth //指定要跳转的组件
  }]
})

传递参数和接收参数看下边代码
传递参数

<template>
  <ul>
    <li>
      <router-link :to="{name:'helloworld',params:{worldmsg:'你好世界'}}">Hello World</router-link>
    </li>
    <li>
      <router-link :to="{name:'helloearth',params:{earthmsg:'你好地球'}}">Hello Earth</router-link>
    </li>
  </ul>
</template>

接收参数

<template>
  <div class="hello">
    <h1>Hello Earth</h1>
    <h2>Essential Links</h2>我是传递过来的参数:======
    <h3>{{$route.params.earthmsg}}</h3>
  </div>
</template>
<template>
  <div class="hello">
    <h1>HelloWorld</h1>
    <h2>Essential Links</h2>
    我是传递过来的参数:======<h3>{{$route.params.worldmsg}}</h3>
  </div>
</template>

Axios之get请求详解

  1. 安装
npm install axios

2.引入加载

import axios from 'axios'
Vue.prototype.$http = axios;

4.发送请求

methods: {
    getData() {
      var self = this;
      this.$http
        .get("https://cnodejs.org/api/v1/topics?page=1&limit=15")
        // .then(function(res) {
        //   //此处的this指向的不是当前vue实例
        //   self.items = res.data.data;
        //   console.log(res.data.data);
        // })
        .then(res => {
          this.items = res.data.data;
          console.log(res);
        })
        .catch(function(err) {
          console.log(err);
        });
    }
  }

两种传递参数的方法

.get("https://cnodejs.org/api/v1/topics", {
          params: {
            page: 1,
            limit: 10
          }
        })
.get("https://cnodejs.org/api/v1/topics?page=1&limit=15")

使用post请求大宋请求

1.安装qs插件

npm install qs

2.引入qs插件

import qs from 'qs'

3.添加post方法,注意参数需要使用插件的qs.stringify()

postData() {
      this.$http
        .get("url", qs.stringify())
        .then(res => {
          this.items = res.data.data;
          console.log(res);
        })
        .catch(function(err) {
          console.log(err);
        });
    }

Vuex之store

父组件向子组件传递数据

·1. 利用v-bind绑定一个数据

<son :msg="toSonMsg" @handle="getMsgFromSon"></son>

2 在data中返回数据
``
data: function() {
return {
toSonMsg: "我是你的父亲",
fromSonMsg: ""
};
},

3.子组件调用props获取数据

props: {
msg: {
type: String,
default: ""
}
},

4. 渲染到页面上

----{{msg}}


##### 子组件向父组件传递数据
1.子组件添加一个事件

<button @click="senMsgToFa">向父组件传递数据</button>

2. 在父组件中也添加一个事件

<son @handle="getMsgFromSon"></son>

3.在组件中添加数据

data: function() {
return {
toFatherMsg: "我是你的son"
};
},

4.在子组件中添加methos,

methods: {
senMsgToFa: function() {
this.$emit("handle", this.toFatherMsg);
}
},

5. 在父组件中定义data

data: function() {
return {
fromSonMsg: ""
};
},

6.在父组件中把值赋值给data

methods: {
getMsgFromSon: function(value) {
this.fromSonMsg = value;
}
},

7.把值渲染到页面上

-----------{{fromSonMsg}}


### 如何使用状态路由操作
1.引入vuex,并通过use方法使用它

import Vuex from 'vuex'

Vue.use(Vuex)

2. 第二步: 创建状态仓库

var store = new Vuex.Store({
state: {
num: 88
}
})

3.通过this.$sore.state.XXX直接拿到需要的数据
在个组件中computed方法

computed: {
getNum: function() {
return this.$store.state.num;
}
}

渲染到页面上

我是父组件拿到的全局状态:{{getNum}}



### Vuex的相关操作
vuex状态管理的流程
view———­>actions———–>mutations—–>state————­>view

mutations操作
1.在state中写入mutation

mutations: {
//定义状态改变函数
increase(state) {
state.num++;
},
decrease(state) {
state.num = state.num - 20
}
},

2.在组件的method中写入方法

padd() {
this.$store.commit("increase");
},


actions操作
1,在state中写入actions操作

actions: {
//context为上下文对象
decreaseAction(context) {
//actions中只能对mutation进行操作
context.commit('decrease');
}
},

2.在组件的methods中写入方法

paddaction() {
this.$store.dispatch("decreaseAction");
}


getters操作
1.在state中写入getter代码

getters: {
getNum(state) {
return state.num > 0 ? state.num : 0;

}

}

2.在组件中调用此数据

computed: {
getCount: function() {
return this.$store.getters.getNum;
}
}


注意:
> actions提交的是mutation,而不是直接变更状态
actions可以包含异步操作,但是mutation只能包含同步操作

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,137评论 6 511
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,824评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,465评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,131评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,140评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,895评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,535评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,435评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,952评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,081评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,210评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,896评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,552评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,089评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,198评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,531评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,209评论 2 357

推荐阅读更多精彩内容