## 評論子組件的封裝和引用 1. 在 項目的 創建`src -> sub-components` 目錄; 2. 創建一個標准的vue組件,命名為 `Comment.vue`, 作為我們的評論子組件 3. 哪個頁面需要引用這個評論組件了,就在頁面的 script 節點中,通過 import 的形式,導入此組件 4. 在 頁面的 script 中,通過 `components`屬性注冊導入的評論組件 5. 把注冊的組件名稱,以標簽形式,引入到頁面上,就能夠看到這個評論子組件了; --------------------------------- // 1.導入 Comment.vue 子組件 import comment from "../sub-components/Comment.vue"; //2注冊 components: { // 為當前的 NewsInfo.vue 組件注冊私有的子組件 comment } //3. <!-- 這里評論組件的位置 --> <!-- 父組件向子組件傳值,通過 屬性綁定的形式 --> <comment :newsid="id"></comment>
評論組件--comment.vue
props: ["newsid"] // 接收父組件傳遞過來的新聞Id
//請求數據-----這里的id直接可以引用--不用在data里聲明了
data() {
return {
page: 1, // 默認展示第一頁的評論
cmtlist: [], // 評論數組
msg: "" // 即將發表的評論內容
};
},
methods:{
getComments() {
this.$http.get(
"/api/getcomments/" + this.newsid + "?pageindex=" + this.page
)
.then(res => {
獲取數據
})
}
}
<!-- 評論列表區域 --> <div class="cmt-list"> <div class="cmt-item" v-for="(item, i) in cmtlist" :key="i"> <div class="cmt-item-title">第{{ i+1 }}樓 用戶:{{ item.user_name }} 發表時間:{{ item.add_time | dateFormat }}</div> <div class="cmt-item-body">{{ item.content }}</div> <!-- {{item.content === 'undefined' ? '此用戶很懶,啥也沒說' : item.content}} --> </div> </div>
<style lang="scss" scoped>
textarea {
font-size: 14px;
margin: 0;
}
.cmt-list {
margin-top: 4px;
.cmt-item {
line-height: 30px;
.cmt-item-title {
font-size: 14px;
background-color: #ddd;
}
.cmt-item-body {
font-size: 13px;
text-indent: 2em;
}
}
}
</style>
完整代碼
<template> <div> <h4>發表評論 --- {{ newsid }}</h4> <hr> <textarea placeholder="請輸入要BB的內容(最多吐槽120字)" maxlength="120" v-model="msg"></textarea> <mt-button type="primary" size="large" @click="postMsg">發表評論</mt-button> <!-- 評論列表區域 --> <div class="cmt-list"> <div class="cmt-item" v-for="(item, i) in cmtlist" :key="i"> <div class="cmt-item-title">第{{ i+1 }}樓 用戶:{{ item.user_name }} 發表時間:{{ item.add_time | dateFormat }}</div> <div class="cmt-item-body">{{ item.content }}</div> </div> </div> <mt-button type="danger" size="large" plain @click="loadMore">加載更多</mt-button> </div> </template> <script> // 按需從 MintUI 中,導出需要的 彈框組件 import { Toast } from "mint-ui"; export default { data() { return { page: 1, // 默認展示第一頁的評論 cmtlist: [], // 評論數組 msg: "" // 即將發表的評論內容 }; }, created() { this.getCommentByPage(); }, methods: { async getCommentByPage() { // 根據頁數來獲取評論的數據 const { data } = await this.$http.get( "/api/getcomments/" + this.newsid + "?pageindex=" + this.page ); if (data.status === 0) return (this.cmtlist = this.cmtlist.concat(data.message)); }, loadMore() { // 點擊按鈕,加載更多的評論 // 當觸發這個加載更多方法的時候,應該讓 page 頁碼 + 1 之后,再調用 getCommentByPage 方法 this.page++; this.getCommentByPage(); }, async postMsg() { // 點擊發表評論: // 如果用戶沒有填寫評論內容,則阻止其發表評論 if (this.msg.trim().length <= 0) return Toast("請填寫評論內容!"); // 發表評論的邏輯: const { data } = await this.$http.post( "/api/postcomment/" + this.newsid, { content: this.msg.trim() } ); if (data.status === 0) { // 刷新評論列表 // 1. 再次調用 getCommentByPage() // console.log("ok"); // // 獲取評論之前,先把之前所有的評論清空 // this.cmtlist = []; // this.getCommentByPage(); // 為了防止重新調用 getCommentByPage 方式時候,會把 之前的所有評論清空的問題: // 我們 在客戶端,手動拼接出一個 評論的對象,並把 這個評論對象, unshift 到 cmtlist 中 this.cmtlist.unshift({ user_name: "匿名用戶", add_time: new Date(), content: this.msg.trim() }); this.msg = ""; } } }, props: ["newsid"] // 接收父組件傳遞過來的新聞Id }; </script> <style lang="scss" scoped> textarea { font-size: 14px; margin: 0; } .cmt-list { margin-top: 4px; .cmt-item { line-height: 30px; .cmt-item-title { font-size: 14px; background-color: #ddd; } .cmt-item-body { font-size: 13px; text-indent: 2em; } } } </style>