1、CourseDetail 课程详细信息

1.如何传入参数id

(1)router中导入

(2) router-link 关联子组件

(3)detail.vue接受id

(4)通过id查询详细

 

2.图片显示

(1)图片路径与效果

 

(2)v-for循环图片路径

3.ajax请求:点击跳转

(1)后端json数据

(2)ajax请求

(3)效果图

2、推荐课程切换

1. url 跳转了,id没有跳转

detail组件不会重写加载,init不会执行

2.主动重定向:改变url和页面id

3、代码与总结

1.Detail组件代码

<template>
<div>
<h1>课程详细页面</h1>
<p>{{detail.title}}</p>
<p>{{detail.img}}</p>
<p>{{detail.level}}</p>
<p>{{detail.course}}</p>
<p>{{detail.slogon}}</p>
<p>{{detail.why}}</p> <div>
<p>章节</p>
<ul v-for="item in detail.chapter">
<li>{{item.name}}</li>
</ul>
</div> <div>
<p>推荐课程</p>
<ul v-for="item in detail.recommends">
<!--<li><router-link :to="{name:'detail',params:{id:item.id}}">{{item.title}}</router-link></li>-->
<li @click="changeDetail(item.id)">{{item.title}}</li>
</ul>
</div> </div>
</template> <script>
export default {
name:"detail",
data(){
return {
detail:{
title:null,
img:null,
level:null,
recommends:[],
chapter:[],
course:null,
slogon:null,
why:null,
}
}
},
mounted(){
// console.log(this.$route.params.id)
var id = this.$route.params.id
this.initDetail(id)
},
methods:{
// 初始化路由id
initDetail(nid){ var that = this this.$axios.request({
url:"http://127.0.0.1:8001/api/v1/course/" + nid +'/',
method:'GET'
}).then(function (arg){
console.log(arg.data)
if(arg.data.code === 1000){
that.detail = arg.data.data
}else{
alert(arg.data.error)
} }).catch(function(){ })
},
// 改变页面id
changeDetail(id){
this.initDetail(id)
this.$router.push({name:'detail',params:{id:id}}) // 主动重定向
}
}
}
</script> <style scoped> </style>

2.Course组件代码

<template>
<div>
<!-- 1.版本1 -->
<h1>{{ msg }}</h1>
<ul v-for="row in courseList">
<li>
<router-link :to='{name:"detail",params:{id:row.id}}'>{{ row.title }}</router-link>
</li>
</ul> <!-- 2.版本2 -->
<div v-for="row in courseList">
<div style="width:350px;float:left">
<!-- 1.传统写法 -->
<!-- <img src="@/assets/logo.png" alt=""> --> <!-- 2.v-for -->
<!-- <img src="{{ row.course_img }}" alt=""> --> <!-- 3.跳转链接 -->
<router-link :to='{name:"detail",params:{id:row.id}}'><img src="@/assets/logo.png" alt=""></router-link> <h3><router-link :to='{name:"detail",params:{id:row.id}}'>{{ row.title }}</router-link></h3>
<p>{{row.level}}</p> </div>
</div> </div>
</template> <script>
export default {
name:"course",
data(){
return {
msg:"课程列表",
courseList:[]
}
},
mounted:function(){
// vue页面刚加载时自动执行
this.initCourse()
},
methods:{
initCourse:function(){
// 通过ajax向接口发送请求,并获取课程列表
// jquery/axios // npm install axios --save
//第一步:在main.js中配置
//第二步:使用axios发送请求 var that = this this.$axios.request({
//参数
url:"http://127.0.0.1:8001/api/v1/course",
method:"GET" }).then(function(ret){
//ajax请求发送成功后,获取响应的内容
console.log(ret)
if(ret.data.code === 1000){
that.courseList = ret.data.data
}else{
alert("获取数据失败")
} }).catch(function(){
//ajax请求失败后,获取响应的内容
})
}
}
}
</script> <style scoped> </style>

3.router/index.js

import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld' import Index from "@/components/Index"
import Course from "@/components/Course"
import Micro from "@/components/Micro"
import News from "@/components/News"
import Detail from "@/components/Detail" Vue.use(Router) export default new Router({
routes: [
{
path: '/index',
name: 'index',
component: Index
},
{
path: '/course',
name: 'course',
component: Course
},
{
path: '/micro',
name: 'micro',
component: Micro
},
{
path: '/news',
name: 'news',
component: News
},
{
path: '/detail/:id',
name: 'detail',
component: Detail
},
],
'mode':'history'
})

4.作业

03 Vue -课程详细(传参id)、图片显示、推荐课程(主动重定向)的更多相关文章

  1. 18 vue 动态路由传参

    params形式 http://192.168.1.100:8080/#/infoDetailed/231 //定义路由{ path: "/infoDetailed/:newsId" ...

  2. vue.js 1中父组件跳到子组件中并传参让子组件显示不同的内容

    父组件中的点击跳转: <ul class="insurance-entry clearfloat"> <li v-link="{name:'produc ...

  3. vue 父子组件传参

    父向子组件传参 例子:App.vue为父,引入componetA组件之后,则可以在template中使用标签(注意驼峰写法要改成componet-a写法,因为html对大小写不敏感,component ...

  4. webpack+vue 组件间传参(单一事件中心管理组件通信--$root),如果有路由的话会失效

    先给一个例子: <body> <div id="box"> <com-a></com-a> <com-b></co ...

  5. vue子组件传参给父组件

    关于父组件传参给子组件,可以看我另一篇文章 教程开始: 我们要实现的效果是:在子组件的Input框输入,父组件中实时更新显示.(也就是把子组件中的数据传给父组件) 一.子组件代码 template部分 ...

  6. vue父组件传参给子组件

    其实组件之间传参有很多种方法: 1.通过本地存储 2.使用vuex状态管理 今天记录一下第三种方法 1.首页我们先创建一个项目(创建项目自行百度) 2.打开项目,在components文件夹下新建一个 ...

  7. vue路由组件传参

    在组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性. 使用 props 将组件和路由解耦: 取代与 $route 的耦合 const ...

  8. vue之路由传参三种基本方式

    现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据. 父组件中: <li v-for="article in articles" @click= ...

  9. vue中路由传参的方式

    一.params的类型: 配置路由格式: /router/:id 传递的方式: 在path后面跟上对应的值 传递后形成的路径: /router/123, /router/abc 通过:to字符串拼接的 ...

随机推荐

  1. ArrayList集合详解

    ArrayList 实现了List的接口,是长度可变的数组,空间是连续的 api默认提供了很多操作ArrayLis的方法,这些方法可以去api里面查询使用 一.这么多方法怎么学?1.熟练使用常见的方法 ...

  2. ES6——入门学习指南

    ES6的简介: ECMAScript6.0(以下简称ES6)是JavaScript语言的下一代标准,已在2015年6月正式发布了.它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序 ...

  3. 第6章:使用Python监控Linux系统

    1.Python编写的监控工具 1).多功能系统资源统计工具dstat dstat是一个用Python编写的多功能系统资源统计工具,用来取代Linux下的vmstat,iostat,netstat和i ...

  4. ingress安装配置

    Traefik Traefik 是一款开源的反向代理与负载均衡工具.它最大的优点是能够与常见的微服务系统直接整合,可以实现自动化动态配置.目前支持 Docker.Swarm.Mesos/Maratho ...

  5. 关于DB2的使用(DB2数据命令)

           公司所用的数据库有金仓和DB2 首先要用命令窗口直接打开db2需要在cmd中输入:db2cmd 1:启动DB2数据库:db2start 2:连接数据库:db2 connect to  数 ...

  6. 训练技巧详解【含有部分代码】Bag of Tricks for Image Classification with Convolutional Neural Networks

    训练技巧详解[含有部分代码]Bag of Tricks for Image Classification with Convolutional Neural Networks 置顶 2018-12-1 ...

  7. DataTime.Now.Ticks

    getTime public long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数. 返回: 自 1970 年 1 月 1 ...

  8. 作业10:String类

    一.基本案例 1.new String("helloworld") 与 "helloworld" public static void main(String[ ...

  9. Windows编程 Windows程序的生与死(下)

    再谈程序之“死” 记得在第二回中我对程序的“死”只是一句话带过,因为我还没有铺垫好,好了现在我们可以详细的分析一下这个过程了. 这还要从while消息循环说起,还记得GetMessage函数吗?它是一 ...

  10. http 协议相关问题

    http 协议相关问题 来源 https://www.cnblogs.com/lingyejun/p/7148756.html 1.说一下什么是Http协议? 对器客户端和 服务器端之间数据传输的格式 ...