卸载旧版本
npm uninstall vue-cli -g
# OR
yarn global remove vue-cli
安装新版本
npm install -g @vue/cli
# OR
yarn global add @vue/cli
检查其版本是否正确 (3.x)
vue --version
vue create hello-world
vue ui
Vue CLI 3 和旧版使用了相同的 vue
命令,所以 Vue CLI 2 (vue-cli
) 被覆盖了。如果你仍然需要使用旧版本的 vue init
功能,你可以全局安装一个桥接工具:
npm install -g @vue/cli-init
# `vue init` 的运行效果将会跟 `vue-cli@2.x` 相同
vue init webpack my-project
npm run serve
运行项目后 在浏览器的控制台报错Invalid Host/Origin header
解决方法:
在项目的根目录新建一个vue.config.js
文件
module.exports = {
devServer: {
disableHostCheck: true, // 新增该配置项
}
}
用户的配置文件会和默认的配置文件合并 所以不用担心系统默认配置要再配置一遍
安装
npm install --save axios vue-axios
main.js
中添加
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
然后页面中就可以这样请求
Vue.axios.get(api).then((response) => {
console.log(response.data)
})
this.axios.get(api).then((response) => {
console.log(response.data)
})
this.$http.get(api).then((response) => {
console.log(response.data)
})
其实 上面的就相当于
安装
npm install --save axios
main.js
中添加
import axios from 'axios'
Vue.prototype.axios = axios;
然后页面中就可以这样请求
Vue.axios.get(api).then((response) => {
console.log(response.data)
})
在项目的根目录新建一个vue.config.js
文件
module.exports = {
devServer: {
disableHostCheck: true, // 新增该配置项
proxy: {
'/psvmc': {
target: 'http://www.psvmc.cn', //代理接口
changeOrigin: true,
pathRewrite: {
'^/psvmc': '' //代理的路径
}
}
}
}
};
假如我们请求这样写
this.axios
.get('/psvmc/userlist.json')
.then(function (data) {
console.info(data);
})
就能在本地请求到http://www.psvmc.cn/userlist.json
了
用VueX来缓存登录用户的时候,如果页面刷新后,VueX的缓存也会清空 ,所以我们还得用Cookie来保存
安装
npm install vue-cookies --save
配置
import Vue from "vue";
import VueCookies from 'vue-cookies'
Vue.use(VueCookies);
Vue.prototype.$mycookie = {
addcookie: function (key, value) {
var host = window.location.host;
if (host.indexOf("xhkjedu") !== -1) {
window.$cookies.set(key, value, '7d', '/', '.xhkjedu.com', false);
} else {
window.$cookies.set(key, value, '7d', '/');
}
},
delcookie: function (key) {
window.$cookies.remove(key);
},
getcookie: function (key) {
return window.$cookies.get(key);
}
};
使用
this.$mycookie.addcookie("loginuser",JSON.stringify(data.obj));
我这里封装是为了设置开发和上线后的域不同,所以封装了一下
npm run lint --fix