16.动态路由传值和get传值

1.动态路由传值

1.在components目录下新建vContent.vue组件

<template>
    <div>
        <h2>{{msg}}</h2>

    </div>
</template>
<script>
export default {
  data () {
    return {
        msg:‘详情组件‘,
    }
  },
  methods:{
  },
  mounted(){
      console.log(this.$route.params);//获取动态路由传值
  }

}
</script>
<style lang="scss" scoped>
h2{
    color: red;
}
</style>

2.在main.js中

加入:

{path:‘/vcontent/:aid‘,component:vContent}, //动态路由

main.js:

import Vue from ‘vue‘;
import App from ‘./App.vue‘;

import VueResource from ‘vue-resource‘;
Vue.use(VueResource)

import VueRouter from ‘vue-router‘;
Vue.use(VueRouter)

// 1.创建组件,导入组件
import Home from ‘./components/Home.vue‘;
import News from ‘./components/News.vue‘;
import vContent from ‘./components/vContent.vue‘;
// 2.配置路由
const routes=[
    {path:‘/home‘,component:Home},
    {path:‘/news‘,component:News},

      {path:‘/vcontent/:aid‘,component:vContent}, //动态路由

      {path:‘*‘,redirect:‘/home‘} //默认路由跳转到首页
]
//注意,这里是routes,而不是routers

// 3.实例化VueRouter
const router=new VueRouter({
    routes//(缩写)相当于routers:routers
})

// 4.挂载
new Vue({
  el: ‘#app‘,
  router,
  render: h => h(App)
})

// 5.将<router-view></router-view>放在App.vue里面

3.在News.vue中

<template>
    <div>
        <h2>{{msg}}</h2>
        <ul>
            <li v-for="(item,index) in list" :key=index>
                <router-link :to="‘/vcontent/‘+index">
                    {{index}}---{{item}}
                </router-link>
            </li>
        </ul>
    </div>
</template>
<script>
export default {
  name: ‘news‘,
  data () {
    return {
        msg:‘新闻组件‘,
        list:[‘111‘,‘222‘,‘333‘]
    }
  },
  methods:{

  },

}
</script>
<style lang="scss" scoped>
h2{
    color: red;
}
</style>
  

动态路由传值:

1.配置动态路由

配置路由
const routes=[
      {path:‘/vcontent/:aid‘,component:vContent}, //动态路由
]

2.在对应的页面

this.$route.params //获取动态路由传过来的值

2.get传值

1.在components目录下新建Goods.vue组件

<template>
    <div>
        <h2>{{msg}}</h2>

    </div>
</template>
<script>
export default {
  name: ‘goods‘,
  data () {
    return {
        msg:‘商品详情‘,

    }
  },
  methods:{
  },
  mounted(){
      console.log(this.$route.query);
  }

}
</script>
<style lang="scss" scoped>
h2{
    color: red;
}
</style>

2.首页组件Home.vue

<template>
    <div>
        <h2>{{msg}}</h2>
        <br>
        <ul>
            <li v-for="(item,index) in list" :key=index>
                <router-link :to="‘/goods?aid=‘+index">
                    {{index}}---{{item}}
                </router-link>
            </li>
        </ul>
        <br>
    </div>
</template>
<script>

export default {
  name: ‘home‘,
  data () {
    return {
        msg:‘首页组件‘,
        list:[‘商品111‘,‘商品222‘,‘商品333‘]
    }
  },
  methods:{

  },
  components:{
  }
}
</script>
<style lang="scss" scoped>
h2{
    color: red;
}
</style>

3.在main.js中引用配置

import VueResource from ‘vue-resource‘;
Vue.use(VueResource)

import VueRouter from ‘vue-router‘;
Vue.use(VueRouter)

// 1.创建组件,导入组件
import Home from ‘./components/Home.vue‘;
import News from ‘./components/News.vue‘;
import vContent from ‘./components/vContent.vue‘;

import Good from ‘./components/Goods.vue‘;

// 2.配置路由
const routes=[
    {path:‘/home‘,component:Home},
    {path:‘/news‘,component:News},

      {path:‘/vcontent/:aid‘,component:vContent}, //动态路由

      {path:‘*‘,redirect:‘/home‘}, //默认路由跳转到首页

      {path:‘/goods‘,component:Good},
]
//注意,这里是routes,而不是routers

// 3.实例化VueRouter
const router=new VueRouter({
    routes//(缩写)相当于routers:routers
})

// 4.挂载
new Vue({
  el: ‘#app‘,
  router,
  render: h => h(App)
})

// 5.将<router-view></router-view>放在App.vue里面

注意,别忘了在根组件App.vue中引用

<template>
  <div id="app">
    <h2>{{msg}}</h2>

    <router-view></router-view>

    <router-link to="/home">首页</router-link>
    <router-link to="/news">新闻</router-link>

  </div>
</template>
<script>

export default {
  name: ‘app‘,
  data () {
    return {
      msg:‘根组件‘
    }
  },
  methods:{
  },

}
</script>
<style>

</style>

原文地址:https://www.cnblogs.com/xuepangzi/p/11701665.html

时间: 2024-08-29 01:32:13

16.动态路由传值和get传值的相关文章

第二百六十三也,Tornado框架-基于正则的动态路由映射分页

Tornado框架-基于正则的动态路由映射分页 分页基本显示数据 第一步.设置正则路由映射配置,(r"/index/(?P<page>\d*)", index.indexHandler),正则匹配访问路径,访问录index/后面可以是可以是0个或多个数字第二步.配置逻辑处理方法,get()方法里显示对应的页面,并传值一个SHUJU全局变量列表到html模板,这个全局变量列表里是字典显示的数据第三步.在html模板里用模板语言,循环这个列表里的字典,显示到表格里第四步.设置用

vue2.0-路由嵌套、动态路由跳转

1.创建项目 $ vue init webpack vuerouter2   (忽略所有其他语法) $ cd vuerouter2/ $ npm run dev 正常启动,访问http://localhost:8080,链接即可. 2.基础配置 创建样式文件,src/assets/css/basic.scss 引入静态样式文件,在main.js中引入, import './assets/css/basic.scss' 3.配置基础路由 目录结构如下,包含路由走向. import Router f

angular中的动态路由

1.配置动态路由 const routes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'news', component: NewsComponent}, {path: 'newscontent/:id', component: NewscontentComponent}, { path: '', redirectTo: '/home', pathMatch: 'full' } ]; 2.跳转传值 <a [route

iOS中多视图的传值 属性传值和代理传值

首先创建两个类 ,FirstViewController和SecondViewController,都继承于UIViewController 1 #import "AppDelegate.h" 2 #import "FirstViewController.h" 3 4 @interface AppDelegate () 5 6 @end 7 8 @implementation AppDelegate 9 10 11 - (BOOL)application:(UIAp

属性传值,协议传值,block传值,单例传值四种界面传值方式

一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N + 1界面传值.而在此基础上,必须知道跳转界面的明确位置及所要传的值的具体类型.在第二个界面中声明所要传值 类型的属性. @interface SecondViewController : UIViewController //声明一个字符串属性来保存第一个界面传过来的字符串内容 @propert

CCNA-4-Cisco动态路由

·动态路由:是能够根据网络结构或流量变化会自我调整的路由 ·分类: 1.距离矢量路由协议(DV):RIP.IGRP    通过路由协议发送路由条目 2.链路状态路由协议(LS):OSPF.IS-IS   没有路由条目,只靠收集链路信息计算出路由 3.混合型路由协议:EIGRP            本质是距离矢量路由协议,但具有链路状态的功能 ·距离矢量路由协议: 通过定期将路由表复制给相邻的路由器并且进行矢量堆加 ·特征: 1.更新形式:采用周期性的完全更新(发送整个路由表,只要是更新的内容都

RIP、OSPF、BGP、动态路由选路协议、自治域AS

相关学习资料 tcp-ip详解卷1:协议.pdf http://www.rfc-editor.org/rfc/rfc1058.txt http://www.rfc-editor.org/rfc/rfc1388.txt http://www.rfc-editor.org/rfc/rfc1247.txt http://www.rfc-editor.org/rfc/rfc1267.txt http://www.rfc-editor.org/rfc/rfc1268.txt http://www.cnpa

动态路由

RIP协议 跳数 30秒广播路由表 最大跳数16跳 动态路由: router(config)#ip routing :启动路由转发 router(config)#router rip :启动RIP路由协议. router(config)#version 2 :使用版本2 router(config-router)#network 192.168.110.0:设置发布路由,设置的是网段 router(config-router)#negihbor :点对点帧中继用. route#show ip p

cisco 动态路由RIP配置

动态路由RIP运用在一个较大的企业网络,在有多个路由器的时候. 动态路由就是网络中路由器之间互相通信,传递路由信息,利用收到的路由信息更新路由表的过程. 动态路由协议分类:  距离矢量路由协议 根据跳数来选择路由 如RIP IGRP  链路状态路由协议 根据度量值选择路由 如OSPF IS-IS RIP使用跳数作为唯一的度量值,最多的跳数为15,16跳视为不可达. 配置过程: 配置pc的ip PC1 PC2 测试PC1和PC2的连接情况,发现不通 router1配置RIP 查看rip配置情况 r