今日內容
前端項目搭建准備流程
- 1. 客戶端項目搭建
- 1.1 創建項目目錄
- 1.2 初始化項目
- 1.3 安裝路由vue-router
- 1.3.1 下載安裝路由組件
- 1.3.2 配置路由
- 1.3.2.1 初始化路由對象
- 1.3.2.2 注冊路由信息
- 1.3.2.3 在視圖中顯示路由對應的內容
- 1.3.3 路由對象提供的操作
- 1.3.3.1 頁面跳轉
- 1.3.3.1.1 router-link標簽
- 1.3.3.1.2
this.$router.push()
跳轉
- 1.3.3.2 參數傳遞
- 1.3.3.2.1 獲取查詢字符串
- 1.3.4.2 獲取路由參數
- 1.3.3.1 頁面跳轉
- 2. ElementUI
- 2.1 快速安裝ElementUI
- 2.2 配置ElementUI到項目中
- 3. 首頁
- 3.1 創建首頁組件
- 3.1.1 創建首頁對應的路由
- 3.1 創建首頁組件
今日內容詳細
1. 客戶端項目搭建
1.1 創建項目目錄
cd 項目目錄[荏苒資訊]
vue init webpack renran
例如,我要把項目保存在桌面下的子目錄renran ~/Desktop/renran,可以如下操作:
cd Desktop/renran
vue init webpack renran_pc
打開項目已經,在pycharm的終端下運行vue項目,查看效果。
上面的操作步驟,等同於執行了下面這句命令。
npm run dev
接下來,我們根據終端上效果顯示的對應地址來訪問項目(如果有多個vue項目在運行,8080端口被占據了,服務器會自動改端口,所以根據自己實際在操作中看到的地址來訪問。)
1.2 初始化項目
清除默認的HelloWorld.vue組件和APP.vue中的默認模板代碼和默認css樣式
<template> <div id="app"> </div> </template> <script> export default { name: 'App', components: { } } </script> <style> </style>
接下來,我們可以查看效果了,一張白紙~
1.3 安裝路由vue-router
官方文檔:https://router.vuejs.org/zh/
1.3.1 下載安裝路由組件
npm i vue-router -S
# npm install vue-router --save
1.3.2 配置路由
1.3.2.1 初始化路由對象
在src目錄下創建routes路由目錄,在router目錄下創建index.js路由文件
index.js路由文件中,編寫初始化路由對象的代碼 .
import Vue from "vue";
import Router from "vue-router";
// 注冊路由組件
Vue.use(Router);
// 導入組件
// import Home from "../components/Home";
// 在vue中@表示src目錄的路徑
import Home from "@/components/Home";
import Login from "@/components/Login";
import User from "../components/User";
// 實例化路由對象,編寫路由列表
export default new Router({
// 路由顯示模式,默認hash,地址欄上面默認帶#,history不會帶#號
mode:"history",
routes:[ // 路由列表
{
path: "/home", // 字符串,訪問url地址
component: Home, // 變量對象,訪問url地址對應的組件
name: "Home", // 字符串吧,訪問url的別名
},
]
})
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' // 導入實例化的路由對象 import router from "@/router/index"; Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', // 掛載路由對象,將來路由對象的所有的屬性或方法,都可以通過vm來操作 router, components: { App }, template: '<App/>' }) // 這里是不能編寫任何js代碼的,因為系統不會執行到這里來
在App.vue組件中,添加顯示路由對應的內容。代碼:
<router-view></router-view>
它的作用調用路由組件:路由組件的作用:識別訪問當前站點的url地址,獲取地址的路徑部分,到路由列表中進行識別判斷
<template> <div id="app"> <!--調用路由組件:路由組件的作用:識別訪問當前站點的url地址,獲取地址的路徑部分,到路由列表中進行識別判斷--> <router-view></router-view> </div> </template> <script> export default { name: 'App', components: { } } </script> <style> </style>
注意:如果在vue創建項目的時候,設置安裝vue-router,則項目會自動幫我們生成上面的router目錄和index.js里面的代碼,以及自動到main.js里面注冊路由對象。
在我們安裝注冊了vue-router組件以后,vue-router在vue項目中會幫我們在全局范圍內所有組件里面創建2個對象給我們使用:
-
this.$router
,可用於在js代碼中進行頁面跳轉。 -
this.$route
,可用於獲取地址欄上面的url參數。
1.3.3.1 頁面跳轉
在vue-router提供的操作中, 進行頁面跳轉有2種方式:
-
使用
<router-link to="url地址">
站內跳轉,ajax跳轉不刷新頁面 -
在
<script>
中使用this.$router.push(url地址)
來跳轉到站內的指定路徑,ajax跳轉不刷新頁面 -
如果想跳轉到別的網站,則需要用到原生js停供的
windows.location.href
="http://www.baidu.com",在
<script>
中還可以使用this.$router.go(整數)
,跳轉到歷史記錄的N頁,負數為上幾頁,正數為下幾頁在
<script>
中還可以使用this.$router.back(整數)
,返回上一頁在
<script>
中還可以使用this.$router.forward(整數)
,前進一頁
1.3.3.1.1 router-link標簽
例如,我們就可以在Home.vue組件中,使用router-link跳轉到User.vue組件中。
routes/index.js,代碼:
// 1. 引入vue和vue-router組件核心對象,並在vue中通過use注冊vue-router組件 import Vue from "vue"; import Router from "vue-router"; Vue.use(Router); // Router是類 // 2. 暴露vue-router對象,並在vue-router里面編寫路由,提供給main.js調用 // 導入組件 // import 組件名 from "../components/組件名" import Home from "../components/Home"; import User from "../components/User"; export default new Router({ mode:"history", // 路由地址的顯示模式: 默認hash,表示地址欄上面出現# routes:[ // { // name:"路由名稱[對應組件的name值,將來用於跳轉頁面]", // path: "訪問url路徑", // component: 組件名 // }, { name:"Home", path: "/", component: Home },{ name:"User", path: "/user", component: User }, ], }); // vue-router除了可以進行組件和url地址的綁定以外,還可以 // 進行不同組件的頁面跳轉,
Home.vue代碼:
<template> <div> 首頁頁面組件 <a href="/user">個人中心</a> <!-- router-link標簽,本質上就是a標簽,只是由vue-router進行加工處理 可以顯示局部頁面刷新,不會重新加載內容,進行ajax跳轉 --> <router-link to="/user">個人中心</router-link> <router-link :to="url">個人中心</router-link>
<router-link :to="{path:'/login'}">使用router-link來跳轉到登錄頁,關聯router/index.js中的path</router-link> <router-link :to="{name:'User'}">個人中心,關聯router/index.js中的name</router-link> 逆向解析 </div> </template> <script> export default { name: "Home", data(){ return { url: "/user", } }, methods:{ } } </script> <style scoped> </style>
<template> <div> 首頁頁面組件 <a href="/user">個人中心</a> <button @click="jump">個人中心</button> </div> </template> <script> export default { name: "Home", data(){ return { url: "/user", } }, methods:{ jump(){ // 開發中可以先進行權限,登錄之類的判斷,然后再進行跳轉 // this.$router.back(); // 返回上一頁,本質上就是 location.back() // this.$router.go(-1); // 返回上一頁,本質上就是 location.go() // this.$router.forward(); // 跳轉到下一頁,本質上就是 location.forward() this.$router.push("/user"); // 跳轉到站內的制定地址頁面中,本質上就是 location.href // 注意,this.$router.push() 不能跳轉到其他網站。如果真的要跳轉外站,則使用location.href="站外地址,記得加上http://協議" } } } </script> <style scoped> </style>
1.3.3.2 參數傳遞
vue-router
提供了this.$route
,可以讓我們接收來自其他頁面的附帶參數。參數有2種:
-
查詢字符串(
query string
),就是地址欄上面?
號后面的參數,例如:
http://localhost:8008/user?name=xiaoming&pwd=123
,這里name=xiaoming&pwd=123
就是查詢字符串參數。 -
路由參數(
router params
),就是地址欄上面路由路徑的一部分,例如:
http://localhost:8080/user/300/xiaoming
,此時,300屬於路由路徑的一部分,這個300就是路由參數.,當然,xiaoming,或者user也可以理解是路由參數,就是看我們的頁面中是否需要接收而已。
1.3.3.2.1 獲取查詢字符串
-
必須先有一個頁面跳轉發送參數。例如,在Home組件中跳轉到User組件中,需要傳遞name和pwd查詢字符串。
Home.vue代碼:
<template> <div> <button @click="func1">傳遞查詢字符串參數</button> <router-link :to="`/user?name=${name}&pwd=${pwd}`">查詢字符串參數</router-link> <router-link :to="'/user?name='+name+'&pwd='+pwd">查詢字符串參數</router-link> <router-link :to="{path:'/login',query:{'name':'xiaoming','pwd':'123'}}">傳遞查詢字符串參數</router-link> <router-link :to="{name:'Login',query:{'name':'xiaoming','pwd':'123'}}">傳遞查詢字符串參數</router-link> </div> </template> <script> export default { name: "Home", data(){ return { name: "xiaoming", pwd: "123", url: "/user", } }, methods:{ func1(){ this.$router.push("/login?name=xiaoming&pwd=123"); } }, } </script> <style scoped> </style>
可以下一個頁面中,這里代表的就是User組件,接收來自Home組件的參數。
<template> <div> 用戶中心頁面組件 </div> </template> <script> export default { name: "User", created() { // 需要提前接收參數或者接收服務端數據的初始化代碼都寫在created,此時頁面還沒有出來 // this.$route的其他屬性 console.log(this.$route.fullPath); // /login?name=xiaoming&pwd=123 除了域名地址后面的所有內容 console.log(this.$route.path); // /login 去掉參數以后的路徑 // 接收地址欄上面的參數 // this.$route是vue-router提供的一個用於接收地址參數的對象。 // 經過main.js里面注冊router對象以后, // 將來在所有的子組件中,可以通過this.$route來獲取參數或者通過this.$router跳轉頁面 // 查詢字符串參數 // query是this.$route里面的一個數組,this.$route會自動收集地址欄上所有的參數保存到query里面 // let name = this.$route.query.name; // let pwd = this.$route.query.pwd; // console.log(`name=${name}&pwd=${pwd}`); // ``里面,${}圈住的內容會被js當成變量來解析 } } </script> <style scoped> </style>
1.3.4.2 獲取路由參數
例如:我們用戶的界面都是一樣的,但是每一個用戶來到自己的頁面中,顯示的內容肯定都是不一樣的,此時,我們需要使用不同的路徑來區分不同的用戶。這時候,可以在路由路徑中使用路由參數表示不同用戶的id
例如:我們就需要設置一個route/index.js中路由信息里面,哪一段路由屬於路由參數。
src/routes/index.js設置路由參數。
// 1. 引入vue和vue-router組件核心對象,並在vue中通過use注冊vue-router組件 import Vue from "vue"; import Router from "vue-router"; Vue.use(Router); // Router是類 // 2. 暴露vue-router對象,並在vue-router里面編寫路由,提供給main.js調用 // 導入組件 // import 組件名 from "../components/組件名" import Home from "../components/Home"; import User from "../components/User"; export default new Router({ mode:"history", // 路由地址的顯示模式: 默認hash,表示地址欄上面出現# routes:[ // { // name:"路由名稱[對應組件的name值,將來用於跳轉頁面]", // path: "訪問url路徑", // component: 組件名 // }, { name:"Home", path: "/", component: Home },{ name:"User", path: "/user/:id/img-:img_id",// 冒號:聲明當前地址的2個路由參數名!!! component: User // 自動導包 Alt+Enter }, ], }); // vue-router除了可以進行組件和url地址的綁定以外,還可以 // 進行不同組件的頁面跳轉,
然后我們就是在Home中如果需要轉到User里面。
Home.vue代碼:
<template> <div> <router-link to="/user/100/img-10086">路由參數</router-link> </div> </template> <script> export default { name: "Home", } } } </script> <style scoped> </style>
<template> <div> 用戶中心頁面組件 </div> </template> <script> export default { name: "User", created() { // 路由參數 // params是this.$route里面的一個數組,this.$route會自動收集路由列表中已經標記為路由參數所有內容保存到params中 let id = this.$route.params.id; console.log(id); let img_id = this.$route.params.img_id; console.log(`img_id = ${img_id}`); } } </script> <style scoped> </style>
2. ElementUI
對於前端頁面布局,我們可以使用一些開源的UI框架來配合開發,常用的UI框: bootstrap,H-ui框架,lay-UI框架,Amaze UI,zui框架,ElementUI.
Vue開發前端項目中,比較常用的就是ElementUI了。
ElementUI是餓了么團隊開發的一個UI組件框架,這個框架提前幫我們提供了很多已經寫好的通用模塊,我們可以在Vue項目中引入來使用,這個框架的使用類似於我們前面學習的bootstrap框架,也就是說,我們完全可以把官方文檔中的組件代碼拿來就用,有定制性的內容,可以直接通過樣式進行覆蓋修改就可以了。
中文官網:http://element-cn.eleme.io/#/zh-CN
文檔快速入門:http://element-cn.eleme.io/#/zh-CN/component/quickstart
2.1 快速安裝ElementUI
項目根目錄執行以下命令:
npm i element-ui -S
上面的命令等同於 npm install element-ui --save
執行命令效果:
2.2 配置ElementUI到項目中
在main.js中導入ElementUI,並調用。代碼:
// elementUI 導入 import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; // 調用插件 Vue.use(ElementUI);
成功引入了ElementUI以后,接下來我們就可以開始進入前端頁面開發,首先是首頁。
3. 首頁
首頁采用了上下頁面布局,首頁是導航欄、輪播圖。。。腳部等幾個小模塊。所以我們可以把首頁作為一個組件進行開發,然后把首頁的這些小模塊作為單獨的組件來進行開發。
3.1 創建首頁組件
在src/components目錄下創建文件 Home.vue
代碼:
<template> <div id="home"> 首頁 </div> </template> <script> export default { name:"Home", data(){ return { } } } </script> <style scoped> </style>
3.1.1 創建首頁對應的路由
在router/index.js中引入Home組件,並設置Home組件作為首頁路由。
代碼:
import Vue from "vue" import Router from "vue-router" // 后面這里引入可以被用戶訪問的頁面組件 import Home from "../components/Home" Vue.use(Router); export default new Router({ // 路由跳轉模式,注意使用 history mode: "history", // 路由規則 routes:[ { // name:"路由別名", name:"Home", // path: "路由地址", path: "/", // component: 組件類名, component: Home, }, ] })
作業
把elementUI和vue-router安裝到vue里面,並對於todolist和商品管理的功能,按鈕,輸入框,刪除提示進行UI美化,使用ElementUI提供的樣式和特效進行加工。 要求: 1. todolist和商品管理是2個獨立的頁面組件,有屬於自己的url地址,相互之間可以通過按鈕進行頁面跳轉. 2. 每一個頁面組件至少加入3個以上的ElementUI的效果。