Vue路由跳转

路由跳转

this.$router.push(‘/course‘);
this.$router.push({name:‘course‘})

this.$router.go(-1)   // js逻辑使用history,完成返回上一页
this.$router.go(1)    // 前进一页

<router-link to="/course">课程页</router-link>
<router-link :to="{name:‘course‘}">课程页</router-link>

路由传参

第一种

router.js

routes: [
    // ...
    {
        path: ‘/course/:id/detail‘,
        name: ‘course-detail‘,
        component: CourseDetail
    },
]

跳转.vue

<template>
    <router-link :to="`/course/${course.id}/detail`">{{ course.name }}</router-link>
</template>
<script>
    // ...
    goDetail() {
        this.$router.push(`/course/${this.course.id}/detail`);
    }
</script>

接受.vue

created() {
    let id = this.$route.params.id;
}

第二种

router.js

routes: [
    // ...
    {
        path: ‘/course/detail‘,
        name: ‘course-detail‘,
        component: CourseDetail
    },
]

跳转.vue

<template>
    <router-link :to="{
            name: ‘course-detail‘,
            query: {id: course.id}
        }">{{ course.name }}</router-link>
</template>
<script>
    // ...
    goDetail() {
        this.$router.push({
            name: ‘course-detail‘,
            query: {
                id: this.course.id
            }
        });
    }
</script>

接受.vue

created() {
    let id = this.$route.query.id;
}

store

export default new Vuex.Store({
    state: {
        title: ‘默认值‘
    },
    mutations: {
        // mutations 为 state 中的属性提供setter方法
        // setter方法名随意,但是参数列表固定两个:state, newValue
        setTitle(state, newValue) {
            state.title = newValue;
        }
    },
    actions: {}
})

赋值
this.$store.state.title = ‘newTitle‘
this.$store.commit(‘setTitle‘, ‘newTitle‘)

取值
console.log(this.$store.state.title)

vue-cookie插件

安装

>: cnpm install vue-cookies

main.js 配置

// 第一种
import cookies from ‘vue-cookies‘      // 导入插件
Vue.use(cookies);                    // 加载插件
new Vue({
    // ...
    cookies,                        // 配置使用插件原型 $cookies
}).$mount(‘#app‘);

// 第二种
import cookies from ‘vue-cookies‘    // 导入插件
Vue.prototype.$cookies = cookies;    // 直接配置插件原型 $cookies

使用

// 增(改): key,value,exp(过期时间)
// 1 = ‘1s‘ | ‘1m‘ | ‘1h‘ | ‘1d‘
this.$cookies.set(‘token‘, token, ‘1y‘);

// 查:key
this.token = this.$cookies.get(‘token‘);

// 删:key
this.$cookies.remove(‘token‘);

注:cookie一般都是用来存储token的

// 1) 什么是token:安全认证的字符串
// 2) 谁产生的:后台产生
// 3) 谁来存储:后台存储(session表、文件、内存缓存),前台存储(cookie)
// 4) 如何使用:服务器先生成反馈给前台(登陆认证过程),前台提交给后台完成认证(需要登录后的请求)
// 5) 前后台分离项目:后台生成token,返回给前台 => 前台自己存储,发送携带token请求 => 后台完成token校验 => 后台得到登陆用户

axios插件

安装

>: cnpm install axios

main.js配置

import axios from ‘axios‘    // 导入插件
Vue.prototype.$axios = axios;    // 直接配置插件原型 $axios

使用

this.axios({
    url: ‘请求接口‘,
    method: ‘get|post请求‘,
    data: {post等提交的数据},
    params: {get提交的数据}
}).then(请求成功的回调函数).catch(请求失败的回调函数)

跨域问题(同源策略)

// 后台接收到前台的请求,可以接收前台数据与请求信息,发现请求的信息不是自身服务器发来的请求,拒绝响应数据,这种情况称之为 - 跨域问题(同源策略 CORS)

// 导致跨域情况有三种
// 1) 端口不一致
// 2) IP不一致
// 3) 协议不一致

// Django如何解决 - django-cors-headers模块
// 1) 安装:pip3 install django-cors-headers
// 2) 注册:
INSTALLED_APPS = [
    ...
    ‘corsheaders‘
]
// 3) 设置中间件:
MIDDLEWARE = [
    ...
    ‘corsheaders.middleware.CorsMiddleware‘
]
// 4) 设置跨域:
CORS_ORIGIN_ALLOW_ALL = True

element-ui插件

安装

>: cnpm i element-ui -S

main.js配置

import ElementUI from ‘element-ui‘;
import ‘element-ui/lib/theme-chalk/index.css‘;
Vue.use(ElementUI);

使用

依照官网 https://element.eleme.cn/#/zh-CN/component/installation api

原文地址:https://www.cnblogs.com/KrisYzy/p/11657766.html

时间: 2024-08-29 13:41:54

Vue路由跳转的相关文章

vue路由跳转的方式

vue路由跳转有四种方式 1. router-link 2. this.$router.push() (函数里面调用) 3. this.$router.replace() (用法同push) 4. this.$router.go(n) 一.不带参 1.1 router-link <router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行,

详解vue 路由跳转四种方式 (带参数)

详解vue 路由跳转四种方式 (带参数):https://www.jb51.net/article/160401.htm 1.  router-link ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1. 不带参数  <router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建议用name /

vue路由跳转时判断用户是否登录功能

通过判断该用户是否登录过,如果没有登录则跳转到login登录路由,如果登录则正常跳转. 一丶首先在用户登录前后分别给出一个状态来标识此用户是否登录(建议用vuex): 简单用vuex表示一下,不会可以自己去官网多看看: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); var state = { isLogin:0, //初始时候给一个 isLogin=0 表示用户未登录 }; const mutations = { cha

Vue路由跳转问题记录

最近项目上需要用Vue用来做app,在Vue中使用路由时遇到下面的问题. 路由设置如下: { path:'/tab', component:Tab, children:[{ path:'layoutList', name:'LayoutList', component:LayoutList },{ path:'layoutView/:layoutId', name:'LayoutView', component:LayoutView },{ path:'layoutDetail/:viewId'

vue 路由跳转出现的问题

在 export defaul new Router({ )} 这个路由配置中一定要加mode : 'history' 否者就会在路由前面默认添加# 路由跳转的几种方式: 原文地址:https://www.cnblogs.com/lzzey/p/8461214.html

2种方式解决vue路由跳转未匹配相应路由避免出现空白页面或者指定404页面

https://www.cnblogs.com/goloving/p/9254084.html https://www.cnblogs.com/goloving/p/9254084.html 1.路由全局守卫 在做项目的时候,遇到需要做路由跳转,但当用户输入错误url地址,或是其它非法url路由地址,我们或许会想到跳转至404页面.不管你有没有写一个404页面,当出现未匹配路由都需重新指定页面跳转.可能大家首先想到会是路由重定向,redirect来解决这个问题.但实际上通过redirect是没办

vue 路由跳转,传参

一.直接跳转 //js1.this.$router.push('/ad_new') //html 2.<router-link to="/ad_check"> <div class="top-menu-name">审核</div> </router-link> 二.跳转传参 query传参,参数会显示在url后面?id=? this.$router.push({ path: '/member', query: { id

vue路由跳转的多种方式

1.router-link to 跳转 <router-link to="/child"><button>跳转</button></router-link> 2.this.$router.push("ComponentName") ,通过路由名称跳转 <button @click="go()">跳转</button> go(){ this.$router.push("

vue 路由跳转传参

<li v-for="article in articles" @click="getDescribe(article.id)"> getDescribe(id) { // 直接调用$router.push 实现携带参数的跳转 this.$router.push({ path: `/describe/${id}`, }) this.$route.params.id 父组件中:通过路由属性中的name来确定匹配的路由,通过params来传递参数. this