首页前端开发JavaScriptvue中路由传参以及跨组件传参详解

vue中路由传参以及跨组件传参详解

时间2024-02-01 03:03:03发布访客分类JavaScript浏览688
导读:收集整理的这篇文章主要介绍了vue中路由传参以及跨组件传参详解,觉得挺不错的,现在分享给大家,也给大家做个参考。 路由跳转this.$router.push('/course' ;thi...
收集整理的这篇文章主要介绍了vue中路由传参以及跨组件传参详解,觉得挺不错的,现在分享给大家,也给大家做个参考。

路由跳转

this.$router.push('/course');
this.$router.push({
name: course}
    );
    this.$router.go(-1);
    this.$router.go(1);
    router-link to="/course">
    课程页/router-link>
router-link :to="{
name: 'course'}
    ">
    课程页/router-link>
    

路由传参

方法一

router.js

this.$router.push('/course');
this.$router.push({
name: course}
    );
    this.$router.go(-1);
    this.$router.go(1);
    router-link to="/course">
    课程页/router-link>
router-link :to="{
name: 'course'}
    ">
    课程页/router-link>
    

跳转.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;
}
    

在路由路径中如果写了如:id这样的路由匹配(“:”相当于后端的匹配,“id”就相当于有名分组的名字)。

方法二

router.js

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

跳转.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

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>
    

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

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

vuex仓库插件

store.js配置文件

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') //第二种

第二种要mutations中提供的setter方法,虽然这个方法最终也是将值传到state中,但是我们可以这个setter方法里写一些验证之类的判断

在任意组件中取仓库变量的值

console.LOG(this.$store.state.title)

vue-cookie插件

安装

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

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');
    // 查:keythis.token = this.$cookies.get('token');
    // 删:keythis.$cookies.remove('token');
    

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

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

 (上面提到过用cookie也可以完成跨组件传参,由于cookies中没有设置默认值,所以如果是空值的话,在视图函数中拿到的也是空,所以我们要在视图函数中也设置一个默认值,然后对用cookies传过来的值进行判断如果不是空值的时候,用它替换默认值,然后渲染上去)

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(请求失败的回调函数)

案例

// 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) 安装: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

总结

到此这篇关于vue中路由传参以及跨组件传参的文章就介绍到这了,更多相关vue路由及跨组件传参内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • vue动态路由配置及路由传参的方式
  • vue路由跳转传参数的方法
  • vue路由传参页面刷新参数丢失问题解决方案
  • 3种vue路由传参的基本模式
  • 浅谈vue-router 路由传参的方法
  • vue路由传参的基本实现方式小结【三种方式】
  • vue-router2.0 组件之间传参及获取动态参数的方法
  • Vue.js父与子组件之间传参示例

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

上一篇: vue cli3 实现分环境打包的步骤下一篇:Nodejs实现定时爬虫的完整实例猜你在找的JavaScript相关文章 html font标签如何设置字体大小?html font标签属性用法介绍2022-05-16vue3+TypeScript+vue-router的使用方法2022-04-16vue3获取当前路由地址2022-04-16如何利用React实现图片识别App2022-04-16JavaScript展开运算符和剩余运算符的区别详解2022-04-16微信小程序中使用vant框架的具体步骤2022-04-16Vue elementUI表单嵌套表格并对每行进行校验详解2022-04-16如何利用Typescript封装本地存储2022-04-16微信小程序中wxs文件的一些妙用分享2022-04-16JavaScript的Set数据结构详解2022-04-16 其他相关热搜词更多phpjavapython程序员loadpost-format-gallery

若转载请注明出处: vue中路由传参以及跨组件传参详解
本文地址: https://pptw.com/jishu/594835.html
Nodejs实现定时爬虫的完整实例 C语言中system()函数怎么用?

游客 回复需填写必要信息