首页 > 其他分享 >vuex使用,Router的使用,多级路由

vuex使用,Router的使用,多级路由

时间:2023-06-12 20:12:25浏览次数:32  
标签:index vue name userinfo import Router vuex 路由

vuex使用:

  vuex :状态管理器--->存数据(变量)的地方,所有组件都可以操作

 1.概念

    在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信。

  基本使用:

    1.在HomeView.vue中操作state中值

    HomeView.vue:

<template>
  <div class="home">
    <h1>vuex的使用</h1>

    购物车商品数量--------->{{this.$store.state.num}}  //可以直接查询
    <hr>
    <button @click="handlclick">点我购物车加一</button>
  </div>
</template>

<script>
// @ is an alias to /src


export default {
  name: 'HomeView',
  methods:{
    handlclick(){
      //1.直接操作
      this.$store.state.num+=1
    }
  }

}
</script>

    store------index中:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    num:10
  },
  mutations: {
  },
  actions: {
  },

})

    2.通过dispatch触发actions:

    HomeView.vue:

<template>
  <div class="home">
    <h1>vuex的使用</h1>

    购物车商品数量--------->{{this.$store.state.num}}
    <hr>
    <button @click="handlclick">点我购物车加一</button>
  </div>
</template>

<script>
// @ is an alias to /src


export default {
  name: 'HomeView',
  methods:{
    handlclick(){
      //2.通过dispatch触发actions
      this.$store.dispatch('add',2)   //add必须是actions中的方法
    }
  }

}
</script>

    store------index中:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    num:10
  },
  mutations: {
    madd(state,count){
      state.num=state.num+count
    }
  },
  actions: {
    add(context,count){
      //使用commit触发mutations中的函数
      context.commit('madd',count)    //madd为mutations的函数,count为HomeView.vue传来的参数
    }
  },

})

Router的使用:

  about页面跳转到index页面中:

  方式一:使用 router-link 标签,to 地址

<template>
  <div class="about">
    <h1>首页</h1>
    <router-link  to="/index">
      <button>
        点击跳转到index页面
      </button>
    </router-link>

  </div>
</template>

  方式二:js控制

<template>
  <div class="about">
    <h1>首页</h1>
    <button @click="ClickGo">点击跳转index</button>
  </div>
</template>


<script>
export default {
  name: "About",
  methods:{
    ClickGo(){
      this.$router.push('/index')
    }
  }
}

</script>

  路由跳转时,可以使用对象

  通过对象跳转路由name形式

<template>
  <div class="about">
    <h1>首页</h1>
    <router-link  :to="{name:'index'}">
      <button>
        点击跳转到index页面
      </button>
    </router-link>

  </div>
</template>

  通过对象跳转路由path形式:

<template>
  <div class="about">
    <h1>首页</h1>
    <router-link  :to="{path:'/index'}">
      <button>
        点击跳转到index页面
      </button>
    </router-link>
  </div>
</template>


<script>
export default {
  name: "About",
}

</script>

  对象中可以有query属性,是个对象类型,会把里面的key-value拼到路径后面

<template>
  <div class="about">
    <h1>首页</h1>
    <router-link  :to="{path:'/index',query: {id: 1,age: 19}}">
      <button>
        点击跳转到index页面
      </button>
    </router-link>
  </div>
</template>


<script>
export default {
  name: "About",
}

</script>

  index页面中取出query的参数:this.$route.query

<template>
  <div>
    <h1>第二个首页</h1>
  </div>
</template>

<script>
export default {
  name: "Index",
  created() {
    console.log(this.$route.query)
  }
}
</script>

<style scoped>

</style>

   this.router 的一些方法
    this.$router.push(path): 相当于点击路由链接(可以返回到当前路由界面)
    this.$router.replace(path): 用新路由替换当前路由(不可以返回到当前路由界面)
    this.$router.back(): 请求(返回)上一个记录路由
    this.$router.go(-1): 请求(返回)上一个记录路由
    this.$router.go(1): 请求下一个记录路由

多级路由:

  1 新建一个页面组件(Ydh.vue),配置路由

<template>
  <div>
    <h1>ydh的页面</h1>
    <router-link to="ydh01">
      <button>ydh01</button>
    </router-link>


    <router-link to="ydh02">
      <button>ydh02</button>
    </router-link>

    <router-view>

    </router-view>
  </div>
</template>

<script>
export default {
  name: "Ydh"
}
</script>

<style scoped>

</style>

  2.新建两个页面组件,ydh01.vue,ydh02.vue,配置路由children

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Index from "@/views/Index";
import Ydh from "@/views/Ydh";
import ydh01 from "@/views/ydh/ydh01";
import ydh02 from "@/views/ydh/ydh02";
import Ydh01 from "@/views/ydh/ydh01";
import Ydh02 from "@/views/ydh/ydh02";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView
    },
    {
        path: '/about',
        name: 'about',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    },

    {
        path: '/index',
        name: 'index',
        component: Index
    },

    {
        path: '/ydh',
        name: 'ydh',
        component: Ydh,
        children: [{
            path: 'ydh01',
            component: Ydh01
        },
            {
                path: 'ydh02',
                component: Ydh02
            }
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

export default router

路由守卫(前置路由守卫):

  概念:前置路由守卫,再进入路由之前做判断

  执行流程:写在router-index.js中,以后访问任意一个路由,都会执行这个代码

  router-index.js中:

router.beforeEach((to, from, next) => {
    console.log('前置路由守卫', to, from)
    // 要是访问ydh01,都不能跳转'
    if (to.path == '/ydh/ydh01') {
        alert('你没有权限')
    } else {
        next()
    }
})

localstorage和sessionstorage,和cookie:

localstorage(增删改查):

  localstorage:永久存储,除非你删除。关闭浏览器,再打开还会在

<template>
  <div class="home">
    <h1>操作localstorage的增删改查</h1>
    <button @click="addlocalstorage">增加</button>
    <button @click="deletelocalstorage">删除</button>
    <button @click="getlocalstorage">查看</button>

  </div>
</template>

<script>
// @ is an alias to /src


export default {
  name: 'HomeView',
  methods: {
    addlocalstorage() {
      var userinfo = {name: 'ydh', age: 18}
      localStorage.setItem('userinfo', JSON.stringify(userinfo))
    },
    deletelocalstorage() {
      localStorage.clear()
    },
    getlocalstorage() {
      var userinfo = localStorage.getItem('userinfo')
      console.log(JSON.parse(userinfo).name)
      console.log(JSON.parse(userinfo).age)
    }
  }
}
</script>

 

sessionstorage:

  sessionstorage:只在当前会话生效,关闭浏览器,就没了

<template>
  <div class="about">
    <h1>操作sessionstorage的增删改查</h1>
    <button @click="addsessionstorage">增加</button>
    <button @click="deletesessionstorage">删除</button>
    <button @click="getsessionstorage">查看</button>
  </div>
</template>


<script>
export default {
  name: "About",
  methods: {
    addsessionstorage() {
      var userinfo = {name: 'ydh', age: 18}
      sessionStorage.setItem('userinfo', JSON.stringify(userinfo))
    },
    deletesessionstorage() {
      sessionStorage.clear()
    },
    getsessionstorage() {
      var userinfo = sessionStorage.getItem('userinfo')
      console.log(JSON.parse(userinfo).name)
      console.log(JSON.parse(userinfo).age)
    }
  }
}

</script>

cookie:

cookie:有过期时间,到了过期时间,自动删除

  需要借助于第三方  vue-cookies

  安装:cnpm install -S vue-cookies

     main.js中:

import cookies  from 'vue-cookies'
Vue.use(cookies)

    cookie.vue中:

<template>
  <div class="about">
    <h1>操作cookie的增删改查</h1>
    <button @click="addcookie">增加</button>
    <button @click="deletecookie">删除</button>
    <button @click="getcookie">查看</button>
  </div>
</template>


<script>
export default {
  name: "About",
  methods: {
  addcookie(){
    this.$cookies.set('name','ydh','5')
  },
    getcookie(){
    console.log(this.$cookies.get('name'))
    },
    deletecookie(){
    this.$cookies.remove('name')
    }
  }
}

</script>

 

标签:index,vue,name,userinfo,import,Router,vuex,路由
From: https://www.cnblogs.com/Hao12345/p/17476003.html

相关文章

  • vue之elementui使用, vuex使用, Router使用, localstorage和sessionstorage,和cookie
    目录一、elementui使用下载插件vue界的ui库二、vuex的使用1.概念2.何时使用?3、使用步骤:三、Router使用1简单使用2组件中实现页面跳转3路由跳转时,可以使用对象4this.router的一些方法四、多级路由五、路由守卫和两种工作模式路由守卫路由器的两种工作模式六、localstorage和se......
  • 这个618,网工最值得买的路由器/交换机设备
    大家好,我是老杨。有小友最近扎堆冒出来问我,企业要采购路由器/交换机,买啥牌子好,买啥型号好。又或者是,家里要买家用的路由器/电脑,啥性价比最高?我真的很想吐槽一句,你们再问下去,我就转型成带货博主算了和编辑部商量了一下,为了满足你的愿望,勉强整出了一篇小小的安利贴。多买多风险,买买需......
  • Vue路由使用总结
    1、多级路由(1)配置路由规则,使用children配置项://编写配置项constrouter=newVueRouter({routes:[{path:'/about',component:About,},{path:'/home',component:Home,children:[{path......
  • vue Router的原理及传参方法
    VueRouter是Vue.js官方的路由管理器,它和Vue.js的核心深度集成,可以非常方便地实现单页面应用程序(SPA)的路由功能。VueRouter的原理主要是通过监听URL的变化,根据不同的URL显示不同的组件,从而实现页面的切换和跳转。具体来说,VueRouter的原理包括以下几个方面:路由配置......
  • 终于理解集线器、交换机、路由器之间的区别了
    集线器、交换机、路由器 什么是集线器Hub?1、把内网中的网络设备连接起来,支持多个以太网连接的端口,可以连接多种网络设备2、仅仅知道端口上是否连接了设备,经过集线器传输的数据包,所有设备都能接收到,如下图,当主机A发送数据包给主机C时,主机B和D都能接收到数据3、不仅带来......
  • 全方位教你无线路由器该怎么买
    没有全面复工复产复学之前,不少朋友都是在家办公,还在也在家上网课,有一个问题就暴露了出来。这个问题平时没怎么觉得重要,现在在家时间长了才发现是个令人头疼的问题。这个问题就是——家中WiFi信号不好。WiFi信号不好,自己远程办公体验差,孩子网课卡顿......,亟需解决方法。通常一般用户......
  • drf之自动生成路由
    自动生成路由的前提是必须继承了ViewSetMixin及其子类的视图类,才能用一路由映射eg:path('books/',BookView.as_view({'get':'list','post':'create'})),path('books/<int:pk>/',BookView.as_view({'get':'r......
  • vuex使用,Router使用(做两个主页面的跳转),路由守卫(对路由进行权限控制),路由的工作模式
    vuex使用使用的流程文件中的代码前端页面<template><div><h1>使用vuex</h1>购物车商品数量:{{num}}购物车的数量:{{$store.state.num}}<br><button@click="yjx">加数量</button></div></template><sc......
  • 详解Angular路由之子路由
    原文:https://www.jb51.net/article/213074.htm一、子路由语法二、实例1、新建2个组件修改其内容2、修改路由配置3、修改product.component.ts的模版一、子路由语法二、实例在商品详情页面,除了显示商品id信息,还显示了商品描述,和销售员的信息。通过子路由实现商品......
  • 路由器学习之MPLS
    1.主机不会发送和接收携带标签的数据包,因而需要特定路由器为数据包添加标签并由其他路由器去除标签,标签压入和弹出动作2.MPLS依赖CEF(思科快速转发),路由器根据动态路由协议、静态路由、直连路由创建RIB(路由信息库),之后创建FIB(转发信息库)。3.LSR(标签交换路由器,labelswitchrouter)......