首页 > 编程语言 > 详细

Python vue路由跳转的几种方式 路由带参跳转 js的for循环补充

时间:2019-10-12 00:03:52      阅读:141      评论:0      收藏:0      [点我收藏+]

路由固定跳转的几种方式

#1,2种一般用于事件函数中
1.this.$router.push(路由地址);
this.$router.push(/course);

2.this.$router.push({name: 路由注册名});
this.$router.push({name: course});

# 相当于a标签
1.<router-link to="路由地址">
<router-link to="/course">课程页</router-link>

2.<router-link :to="{name: ‘路由地址‘}">
<router-link :to="{name: ‘course‘}">课程页</router-link>

#页面前后跳转
this.$router.go(-1);
this.$router.go(1);

jq的for循环补充

#for of 遍历的值 | for in 遍历的是取值的依据(arr是索引,obj是key)

 for (let course of course_list) {
                if (id == course.id) {
                    this.course = course;
                    break
                }
            }
 
 for (let index in course_list) {
                console.log(index)
            }         

 

路由传参跳转的常用方式(推荐第二种方式)

第一种方式(一方路由$router.push跳转时在params或query中添加参数,另一方在$route.params或query中获取值)

router.js

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

跳转.vue(事件函数跳转还是标签跳转都可以选择其中一个)
<template>
    <!-- 事件函数跳转 -->
    <!--<h1 @click="goDetail">{{ course.name }}</h1>-->

    <!-- 标签跳转 -->
    <router-link :to="{
            name: course-detail,
            query: {id: course.id}
        }">{{ course.name }}</router-link>
</template>
<script>
    // ...
mehods:{
  goDetail() {
        // 逻辑跳转
        this.$router.push({
            name: ‘course-detail‘,
            query: {
                id: this.course.id
            }
        });
    }
} </script>
接收.vue
created() {
    let id = this.$route.query.id;
}

第二种方式(路由匹配采用有名分组的形式)

router.js(:id为有名分组)
routes: [
    // ...
    {
        path: /course/:id/detail,
        name: course-detail,
        component: CourseDetail
    },
]
跳转.vue(事件函数跳转还是标签跳转都可以选择其中一个)
<template>
     <!-- 事件函数跳转 -->
     <!--<h1 @click="goDetail">{{ course.name }}</h1>-->

    <!-- 标签跳转 -->
    <router-link :to="`/course/${course.id}/detail`">{{ course.name }}</router-link>
</template>
<script>
    // ...
 methods: {
   goDetail() { // 逻辑跳转 this.$router.push(`/course/${this.course.id}/detail`); } 
}
</script>

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

可以完成跨组件传参的四种方式

// 1) localStorage:永久存储数据
// 2) sessionStorage:临时存储数据(刷新页面数据不重置,关闭再重新开启标签页数据重置)
// 3) vuex的仓库(store.js):临时存储数据(刷新页面数据重置,然后使用原先默认的数据)
// 4) cookie:临时或永久存储数据(由过期时间决定) 

vuex仓库插件

store.js配置文件 
export default new Vuex.Store({
    state: {
        title: 默认值
    },
    mutations: {
        // mutations 为 state 中的属性提供setter方法
        // setter方法名随意,但是参数列表固定两个:state, newValue
        setTitle(state, newValue) {
            state.title = newValue;
        }
    },
    actions: {}
})
在任意组件中给仓库变量赋值(2中方式)
this.$store.state.title = newTitle
this.$store.commit(setTitle, newTitle) # (函数名,要修改的值)
在任意组件中取仓库变量的值
console.log(this.$store.state.title)

vue-cookies插件

安装:pycharm自带的cmd窗口
>: 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插件(前后端交互,类似于ajax)

安装(pycharm自带cmd窗口)
>: 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(请求失败的回调函数)
案例
// get请求
this.$axios({
    url: http://127.0.0.1:8000/test/ajax/,
    method: get,
    params: {
        username: this.username
    }
}).then(function (response) {
    console.log(response)
}).catch(function (error) {
    console.log(error)
});

// post请求
this.$axios({
    url: http://127.0.0.1:8000/test/ajax/,
    method: post,
    data: {
        username: this.username
    }
}).then(function (response) {
    console.log(response)
}).catch(function (error) {
    console.log(error)
});

跨域问题(同源策略)

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

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

// Django如何解决 - django-cors-headers模块
// 1) 安装(django中pycharm自带cmd窗口):pip3 install django-cors-headers
##以下下操作全部在settings.py中操作,还要注释中间件csrf
// 2) 注册:
INSTALLED_APPS = [
    ...
    corsheaders
]
// 3) 设置中间件:
MIDDLEWARE = [
    ...
    corsheaders.middleware.CorsMiddleware
]
// 4) 设置跨域:
CORS_ORIGIN_ALLOW_ALL = True

element-ui插件(类似于boot)

安装:pycharm自带cmd窗口
>: 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

Python vue路由跳转的几种方式 路由带参跳转 js的for循环补充

原文:https://www.cnblogs.com/tfzz/p/11657745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!