前言:
vue里面怎样从后台渲染列表,怎么根据文章的id获取文章的具体内容。以及值与值之间的传递,vue-router 里query和params的区别及使用。
一、query和params
先来看看query和params是怎样传值和接收参数的吧!后面会用到的哦!
(1)query方式传参和接收参数
query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数
传递参数:把数据发送出去
this.$router.push({ path:'/aaa/bbb/ccc', query:{ id:aaaid } })
接收参数:在其他的组件中接收传过来的参数
this.$route.query.id
*注:接收参数是r o u t e "htmlcode">
this.$router.push({ name:'aaa', params:{ id:aaaid } })
接收参数:
this.$route.params.id
注意:params传参,push里面是name不是path!!
二、从后台渲染列表
这里我们要创建一个vue组件,名为ArticleList,用于存放渲染的文章列表。
下面是ArticleList的父组件:
假设叫information
<template> <div class="Information"> <section> <p>文章列表为:</p> <ArticleList :ArticleList_props_Data="ArticleList_props_Data" :project_article_Data="project_article_Data" ></ArticleList> //给子组件传值 </div> </section> </div> </template> <script> import axios from 'axios'; import Qs from 'qs'; import ArticleList from "@/components/ArticleList"; export default { name: "information", components: { ArticleList, }, data() { return { current:'1', ArticleList_props_Data: { current_path: '/index/information' }, project_article_Data: [ { id: ``, title: ``, intro: ``, text: ``, issue_time: ``, source:`` } ] } }, created(){ this.get_project_article_Data() }, methods: { get_project_article_Data() { axios({ url: `/API/aaa/bbb/ccc/project"get", params: { page: this.current, }, transformRequest: [ function (data) { data = Qs.stringify(data); return data; }, ], headers: { "Content-Type": "application/x-www-form-urlencoded;charset=utf-8", }, dataType: "json", }) .then((res) => { console.log("连接成功"); // 这里多打印一句提示,只是为了更直观一点 console.log(res); // res 是后端回传的数据,如果连接成功,可以把res打印出来。 this.project_article_Data=res.data.datas }) .catch(function (error) { console.log("连接失败"); // 作用同上 console.log(error); // 如果连接失败,会抛出错误信息。 }); }, }, } </script> <style scoped lang="scss"> //这里就不写css了 </style>
在ArticleList组件里面写入:
<template> <div class="hello"> <div class="project_content"> <div class="project_article_box" v-for="item in project_article_Data" :key="item.id" > <h1 class="project_article_title" @click="to_article_content(item.id)" > <a href="javascript:" rel="external nofollow" >{{ item.title }}</a> </h1> <p class="project_article_intro">{{ item.intro }}</p> <p class="project_issue_time"> <span>{{item.issue_time}}</span>来源: {{ item.source }} </p> <a-divider /> </div> </div> </div> </template> <script> export default { name: "articlelist", props: { project_article_Data: Array, //注册父组件中导入的数据 ArticleList_props_Data: Object, }, data() { return { }; }, methods: { //根据文章id跳转文章详情 to_article_content(article_id) { this.$router.push({ path: `${this.ArticleList_props_Data.current_path}/article_content`, //根据路径跳转到文章在详情页并给详情页传递id query: { article_id: article_id }, }); }, }, }; </script> <style scoped lang="scss"> </style>
(4)根据id获取文章详情
再创建一个名为“article_content”的vue组件,用来放置文章的详情信息。
acticle_content如下:
<template> <div class="Article_Content"> <section> <div id="content"> <div class="article_container"> <p>article_id:{{ $route.query.article_id }}</p> <p class="article_text_title"> {{article_text_title}} </p> <p class="article_text_message">发布时间:{{article_text_message}}</p> <a-divider /> <p class="article_text_content" v-html="this.article_text_content"> </p> </div> </div> </section> </div> </template> <script> import axios from "axios"; import qs from "qs"; export default { name: "Article_Content", props: { }, data() { return { article_id: this.$route.query.article_id, //通过路径跳转传过来的id article_text_title:"", article_text_message:'', article_text_content:'', }; }, created() { this.get_article_data(this.article_id); }, methods: { /* 功能:获取文章内容 参数:article_id 文章id */ get_article_data(article_id) { //获取传过来的具体id值 axios({ url: `/API/aaa/bbb/${this.article_id}`, // 后端的接口地址 method: "get", params: { //给后台传递id地址 id: this.article_id, }, transformRequest: [ function (data) { data = qs.stringify(data); return data; }, ], headers: { "Content-Type": "application/x-www-form-urlencoded;charset=utf-8", }, dataType: "json", }) .then((res) => { console.log("连接成功"); // 这里多打印一句提示,只是为了更直观一点 console.log(res); // res 是后端回传的数据,如果连接成功,可以把res打印出来。 this.article_text_title = res.data.title this.article_text_message= res.data.issue_time this.article_text_content=res.data.content this.article_category=res.data.article_category }) .catch(function (error) { console.log("连接失败"); // 作用同上 console.log(error); // 如果连接失败,会抛出错误信息。 }); }, }, }; </script>
在index.js中去注册组件(router),注意路径!!!
import information from '@/components/information' import ArticleList from '@/components/ArticleList' import Article_Content from '@/pages/Article_Content' const router = [ { path: '/index/information', name: 'information', component: nformation, }, { path: '/index/information/article_content', name: 'article_content', component: article_content } ]
三、总结
1、params和query的区别及使用
2、根据id获取详细信息,id就藏在点击事件里面,当点击时,就跳转到详情页并把此时传过来的id传给后台,在详情页上根据id获取后台返回的数据并渲染出来。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 苏文劭《雨停出来走走》[FLAC/分轨][210.76MB]
- 群星《2024好听新歌04》十倍音质WAV分轨
- 陈宁《弹指之间HQ》头版限量[低速原抓WAV+CUE]
- 陈宁《故人还》HQCDII限量签名版[低速原抓WAV+CUE]
- 苏文劭《春曲(Lessons)》[320K/MP3][39.8MB]
- 苏文劭《春曲(Lessons)》[FLAC/分轨][97.78MB]
- 群星《2006香港高级视听展原音精选 SACD》[ISO][2G]
- 张琍敏1977-枫林小雨[台湾复刻版][WAV+CUE]
- 林一峰2014-COOKINGMUSIC[香港首版][WAV+CUE]
- 张国荣.2003-一切随风【环球】【WAV+CUE】
- 群星1990-6Pair半+城市民歌白金珍藏版[香港首版][WAV+CUE]
- 群星1990-悲情城市SACD[欧洲版][WAV+CUE]
- 吕继宏2010-北国之恋[中唱][WAV+CUE]
- 《许巍4CD合集》[FLAC][1.3G]
- 群星《全民诵唱》宝丽金50周年之本地篇3CD[WAV+CUE][1.8G]