Vue入門教程 第六篇 (路由、axios)


路由

Vue.js 路由需要載入 vue-router 庫。

npm安裝:

 npm install vue-router 

使用范例:

 1 // router/index.js
 2 import Vue from 'vue'
 3 import Router from 'vue-router'
 4 import Login from '@/pages/login'
 5 import Home from '@/pages/home'
 6 
 7 Vue.use(Router)
 8 const router = new Router({
 9   routes: [
10     {
11       path: '/',
12       name: 'login',
13       component: Login
14     },
15     {
16       path: '/home',
17       name: 'home',
18       component: Home,
19       meta: {//meta是拓展,默認為空
20         privilegeLevel: 1//自定義字段,此處用來表示訪問的權限級別
21       }
22     }
23   ]
24 })
25 
26 export default router;

其中,path是url路徑,name是路由名稱,component是組件,meta是拓展屬性,可以自定義。

 

拓展知識

router為VueRouter的實例,相當於一個全局的路由器對象,利用它我們可以實現頁面跳轉。

不帶參數

1 this.$router.push('/home')
2 this.$router.push({name:'home'})
3 this.$router.push({path:'/home'})

query傳參

1 this.$router.push({name:'home',query: {id:'1'}})
2 this.$router.push({path:'/home',query: {id:'1'}})
3 // html 取參 $ route.query.id
4 // script 取參 this.$ route.query.id

params傳參

 1 this.$ router.push({name:'home',params: {id:'1'}}) // 只能用 name
 2 
 3 // 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
 4 
 5 // 不配置path ,第一次可請求,刷新頁面id會消失
 6 
 7 // 配置path,刷新頁面id會保留
 8 
 9 // html 取參 $ route.params.id
10 
11 // script 取參 this.$ route.params.id

query和params區別:

query類似 get, 跳轉之后頁面 url后面會拼接參數,類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在

params類似 post, 跳轉之后頁面 url后面不會拼接參數 , 但是刷新頁面id 會消失。

 

axios(ajax)

Axios 是一個基於 Promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中。axios作用與ajax一致,但是實用性更強。

npm安裝:
 npm install axios 

Get

 1 axios
 2  .get('https://127.0.0.1/user',
 3  {
 4   params: {//參數
 5           userId:1,
 6           password:123345
 7           }
 8  })
 9  .then(response => {
10  //response是返回結果
11  })
12  .catch(function (error) { // 請求失敗
13    console.log(error);
14  });

Post

 1 axios
 2  .post('https://127.0.0.1/user',
 3  {//參數
 4   userId:1,
 5   password:123345
 6  })
 7  .then(response => {
 8  //response是返回結果
 9  })
10  .catch(function (error) { // 請求失敗
11    console.log(error);
12  });

所有請求方式:

  • axios.request(config)
  • axios.get(url[, config])
  • axios.post(url[, data[, config]])
  • axios.delete(url[, config])
  • axios.head(url[, config])
  • axios.put(url[, data[, config]])
  • axios.patch(url[, data[, config]])

響應結果(response對象):

1 axios.get("/user")
2   .then(function(response) {
3     console.log(response.data);//由服務器提供的響應
4     console.log(response.status);//HTTP 狀態碼
5     console.log(response.statusText);//來自服務器響應的 HTTP 狀態信息
6     console.log(response.headers);//服務器響應的頭
7     console.log(response.config);//為請求提供的配置信息
8   });

axios默認值

當我們需要大量使用axios的時候,如果每次都要書寫冗余的配置將會耗費時間且不易管理,axios允許設置默認配置,可以在調用時不再逐一設置。
全局:

1 //默認訪問的后端服務器url
2 axios.defaults.baseURL = 'http://127.0.0.1:3000/';
3 axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
4 //post的默認數據類型
5 axios.defaults.headers.post['Content-Type'] = 'application/json';

自定義(使用新實例覆蓋全局設置):

1 // 創建實例時設置配置的默認值
2 var instance = axios.create({
3 baseURL: 'http://127.0.0.1:3001/'
4 });
5 instance.defaults.timeout = 2500;
6 instance.get('/longRequest', {
7 timeout: 5000// 為已知需要花費很長時間的請求覆寫超時設置
8 });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM