Vue項目初始化:
Vue項目請求生命周期和檔案元件
建立vue檔案:user
使用者頁面
export default {
name: "User",
components: {
Nav,
Footer,
},
data(){
return {
}
},
methods: {
}
}
1.入口檔案:加載vue、router、store等配置 以及 加載自定義配置(自己的js、css,第三方的js、css)
2.建立項目唯一根元件,渲染App.vue,挂載到index.html中的挂載點 => 項目頁面顯示的就是 App.vue 元件
3.在App.vue中設定頁面元件占位符
4.浏覽器帶着指定 url連結 通路vue項目伺服器,router元件就會根據 請求路徑 比對 映射元件,去替換App.vue中設定頁面元件占位符
* eg: 請求路徑 /user => 要渲染的元件 User.vue => 替換App.vue中的 */
路由
小元件與路由跳轉
寫兩個元件
Footer
這是頁腳
export default {
name: "Footer"
}
.footer {
width: 100%;
height: 120px;
background-color: lightslategrey;
}
.footer p {
line-height: 120px;
text-align: center;
}
Nav
- 首頁
- 使用者
export default {
name: "Nav"
}
.nav {
width: 100%;
height: 60px;
background-color: aquamarine;
list-style: none;
margin: 0;
padding: 0;
}
.nav li {
width: 80px;
text-align: center;
float: left;
}
.nav li:hover {
background-color: antiquewhite;
cursor: pointer;
}
a {
color: black;
text-decoration: none;
display: block;
width: 80px;
line-height: 60px;
}
a.router-link-exact-active {
color: pink;
}
Home.vue導入這兩個元件
// 1、導入要使用的小元件
import Nav from '@/components/Nav.vue'
import Footer from '../components/Footer.vue'
import Book from '../components/Book.vue'
// 前台自己提供的靜态資源,在傳輸後再使用(元件間互動),要才有require函數來加載資源
// let img1 = require('../assets/img/西遊記.jpg');
let books = [
{id:1, img:require('../assets/img/西遊記.jpg'), color:'red', title: '西遊記', info: '西遊記這本書寫的好啊!裡面有妖怪啊,主人公猴子死了啊!!!'},
{id:3, img:require('../assets/img/東遊記.jpg'), color:'orange', title: '東遊記', info: '東遊記這本書寫的好啊!裡面都沒有妖怪啊,有個球啊!!!'},
{id:4, img:require('../assets/img/西廂記.jpg'), color:'cyan', title: '西廂記', info: '西廂記不看過,内容也就那樣吧!!!'},
{id:7, img:require('../assets/img/三國.jpg'), color:'yellow', title: '三國', info: '你看不懂,别看了,我也不講了!!!'},
];
export default {
name: 'home',
// 2、注冊要使用的小元件
components: {
Nav,
Footer,
Book,
},
data() {
return {
books
}
}
}
路由重定向
添加Book檔案:
{{ book.title }}
{{ book.info }}
export default {
props: ['book'],
data() {
return {
color: 'red',
title: '西遊記',
info: '西遊記這本書寫的好啊!裡面有妖怪啊,主人公猴子死了啊!!!'
}
},
methods: {
goDetail(id) {
window.console.log(id);
window.console.log(this.book.id);
// 路由的邏輯跳轉
// window.console.log(this.$router);
// this.$router.push(`/book/detail/${id}`);
// this.$router.push({
// name: 'book-detail',
// params: {pk: id},
// });
}
}
}
.img {
width: 360px;
height: 160px;
}
.left, .right {
float: left;
}
.book:after {
display: block;
content: '';
clear: both;
}
.book {
margin: 10px 0;
border: 1px solid rgba(0,0,0,0.3);
}
.right {
margin-left: 20px;
}
h2 {
cursor: pointer;
}
路有邏輯跳轉:
{{ book.title }}
{{ book.info }}
export default {
props: ['book'],
data() {
return {
color: 'red',
title: '西遊記',
info: '西遊記這本書寫的好啊!裡面有妖怪啊,主人公猴子死了啊!!!'
}
},
methods: {
goDetail(id) {
window.console.log(id);
window.console.log(this.book.id);
// 路由的邏輯跳轉
// window.console.log(this.$router);
// this.$router.push(`/book/detail/${id}`);
// this.$router.push({
// name: 'book-detail',
// params: {pk: id},
// });
}
}
}
.img {
width: 360px;
height: 160px;
}
.left, .right {
float: left;
}
.book:after {
display: block;
content: '';
clear: both;
}
.book {
margin: 10px 0;
border: 1px solid rgba(0,0,0,0.3);
}
.right {
margin-left: 20px;
}
h2 {
cursor: pointer;
}
路由傳參
{{ book.title }}
{{ book.info }}
export default {
props: ['book'],
data() {
return {
color: 'red',
title: '西遊記',
info: '西遊記這本書寫的好啊!裡面有妖怪啊,主人公猴子死了啊!!!'
}
},
methods: {
goDetail(id) {
window.console.log(id);
window.console.log(this.book.id);
// 路由的邏輯跳轉
// window.console.log(this.$router);
// this.$router.push(`/book/detail/${id}`);
// this.$router.push({
// name: 'book-detail',
// params: {pk: id},
// });
}
}
}
.img {
width: 360px;
height: 160px;
}
.left, .right {
float: left;
}
.book:after {
display: block;
content: '';
clear: both;
}
.book {
margin: 10px 0;
border: 1px solid rgba(0,0,0,0.3);
}
.right {
margin-left: 20px;
}
h2 {
cursor: pointer;
}