1. 路由vue-router
1.1 安装使用
- 官方文档:https://router.vuejs.org/zh/
cnpm install vue-router
1.2 项目代码
import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router/index'
Vue.use(ElementUI);
import App from './App.vue'
Vue.config.productionTip = false;
new Vue({
router,
render: h => h(App),
}).$mount('#app')
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import demo from '../components/demo'
import HelloWorld from '../components/HelloWorld'
import login from '../components/login'
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/login', component: login },
{ path: '/hello', component: HelloWorld },
{ path: '/demo', component: demo },
]
})
export default router;
<template>
<div id="app">
<ul>
<!-- to属性默认为path路径,也可以使用重命名name-->
<li><router-link to="/">主页</router-link></li>
<li><router-link to="/login">登陆</router-link></li>
<li><router-link :to="{ name: 'ly' }">布局</router-link></li>
<li><router-link to="/helloworld">欢迎</router-link></li>
</ul>
<!-- 使用路由 -->
<router-view/>
</div>
</template>
<script>
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
padding: 0px 0px;
width: 100%;
height: 100%;
}
</style>
1.3 获取参数
export default {
......
created(){
this.a = this.$route.query.a
console.log(this.a)
}
}
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/helloworld/:id', component: HelloWorld },
]
})
export default {
......
created(){
this.a = this.$route.params.id
console.log(this.a)
}
}
1.4 嵌套路由
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{
path: '/index',
component: index,
children: [
{ path: '', component: demo1 },
{ path: 'demo2', component: demo2 },
{ path: '/demo3', component: demo3 },
]
}
]
})
2. 异步请求axios
2.1 安装使用
npm install axios -S
<template>
<div class="hello">
</div>
</template>
<script>
import axios from 'axios';
export default {
mounted(){
axios.get('https://dog.ceo/api/breeds/image/random')
.then(function (response) {
console.log(response)
})
.catch(function (err) {
console.log(err)
})
}};
</script>
<style>
</style>
<template>
<div class="hello">
<el-image
:src="url"
fit="cover">
</el-image>
</div>
</template>
<script>
import axios from 'axios';
export default {
data(){
return {
url: '',
}
},
mounted(){
axios.get('https://dog.ceo/api/breeds/image/random')
.then(response=>{
console.log(response)
this.url = response.data.message
})
.catch(function (err) {
console.log(err)
});
}
}
</script>
<style>
</style>
2.2 箭头函数
- https://blog.csdn.net/weixin_42218847/article/details/81540996
- https://blog.csdn.net/m0_37686205/article/details/88776259
import axios from 'axios';
var host = 'https://dog.ceo';
export const dogs = () => {
return axios.get(`${host}/api/breeds/image/random`)
};
<template>
<div class="hello">
<el-image
:src="url"
fit="cover">
</el-image>
</div>
</template>
<script>
import {dogs} from '../api/api'
export default {
data(){
return {
url: '',
}
},
mounted(){
dogs()
.then(response=>{
console.log(response)
this.url = response.data.message
})
.catch(function (err) {
console.log(err)
});
}
}
</script>
<style>
</style>
2.3 预检请求
- 非简单请求的CORS请求,会在正式通信之前,增加一次HTTP查询请求,称为"预检"请求,浏览器先询问服务器,当前网页所在的域名是否在服务器的许可名单之中,以及可以使用哪些HTTP动词和头信息字段。只有得到肯定答复,浏览器才会发出正式的XMLHttpRequest请求,否则就报错
- 可参考http://www.ruanyifeng.com/blog/2016/04/cors