title: Vue爬坑date: 2018-07-16 18:34:56
tags: vue
axois请求
let data = _this.$qs.stringify({
'user_name': _this.username,
'password': _this.password
});
_this.$http.post(_this.apiUrl("user/index/login"), data, {'Content-Type': 'application/x-www-form-urlencoded'})
.then((res) => {});
post、put:需要用qs包装data,(url, data, headers:{…})
get、delete: (url, {params:{…}, headers:{…}})
Router实现登陆拦截
先对路由添加meta字段:
{
path: '/',
name: 'Document',
component: Document,
meta: {
// 添加该字段,表示进入这个路由是需要登录的
requireAuth: true,
}
},
对登录页做判断:
// 全局路由跳转拦截
sessionStorage.setItem("token", "");
router.beforeEach(function (to, from, next) {
if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
let token = Vue.prototype.getCookie("token");
if (token) {
if (to.path === '/Login') {
next({
path: '/',
}
)
}
else{
next();
}
}
else {
if (to.path === '/Login') {
next()
}
else {
next({
path: '/Login',
query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由
})
}
}
}
else {
next();
}
});
cookie
//设置cookie
Vue.prototype.setCookie = function (cname, cvalue, exdays) {
let d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
// console.info(cname + "=" + cvalue + "; " + expires);
document.cookie = cname + "=" + cvalue + "; " + expires;
// console.info(document.cookie);
};
//获取cookie
Vue.prototype.getCookie = function (cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1);
if (c.indexOf(name) !== -1) return c.substring(name.length, c.length);
}
return "";
};
//清除cookie
Vue.prototype.clearCookie = function () {
this.setCookie("token", "", -1);
};
//验证cookie
Vue.prototype.checkCookie = function (token = "", expire_time = "") {
let $token = this.getCookie("token");
if ($token !== "") {
// console.log("token " + token);
} else {
if (token !== "" && token != null && expire_time !== "" && expire_time !== null) {
this.setCookie("token", token, 10);
this.setCookie("expire_time", expire_time, 10);
}
}
};
向数组添加指定key的元素
push:
_this.list.push({value:temp[i]["count"], name:temp[i]["category"]});
set:(如果预设为空的话会报错未定义)
_this.$set(_this.list[i], 'value', temp[i]["count"]);
_this.$set(_this.list[i], 'name', temp[i]["category"]);
vue-cli build配置
config/index.js
build相对路径: assetsPublicPath: ‘./‘,
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './',
build/utils.js
资源文件相对路径:publicPath:”../../“
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
publicPath:"../../"
})
} else {
return ['vue-style-loader'].concat(loaders)
}
隐去#
开启history模式:
src/router/index.js
export default new Router({
mode: 'history',
base:'/dist/',
routes: [...]
})
Appache:
并且在打包上传到服务器的dist中添加.htaccess文件,内容为:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^dist\index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dist/index.html [L]
</IfModule>
Nginx:
location / {
try_files $uri $uri/ /dist/index.html;
}
assets和static的区别
assets中的文件会被webpack打包进项目,而static中的文件会直接引用。
assets:对于静态资源(css/js/image/video)要放置在assets中被打包后才正确。
css,js必须以import方式引入,image需要以require方式引入
static:对于类库,字体等文件,需要放在static下。
用Jetbrains打开项目只有文件没有文件夹
删除.idea(本机配置文件)
vue-cli build打包后CSS浏览器兼容前缀自动去除的问题
今天构建发现
原本是很正常的一个兼容性写法渐变。结果npm run build项目时
background: -webkit-linear-gradient(left,#ccc,#fff)
background: -moz-linear-gradient(left,#ccc,#fff);
background: -o-linear-gradient(left,#ccc,#fff);
background: linear-gradient(left,#ccc,@stop);
结果npm run build项目时
background: linear-gradient(left,#ccc,@stop);
只剩下这一行,导致谷歌内核的全看不到样式。
原来是因为autoprefixer 使用了 browserslist 作为依赖
大家改一下看下自己package.json中的,即可。
"browserslist": [
"> 1%",
"last 2 versions",
"last 10 Chrome versions",
"last 5 Firefox versions",
"Safari >= 6",
"ie > 8"
]