如果用 import '/public/css/home.css'
导入 css
,只要访问过的页面,css
就会被保留。
有什么办法能不保留,只保留当前模板导入的 css
如果是 <style src="/public/css/home.css" scoped></style>
那这个只作用于当前,子组件中用不到,有什么好办法,要的效果就是作用于当前模板和子组件
我现在用得一个笨方法,就是在 routes.js
里面加 styles
{
path: '/',
name: 'home',
component: HomeView,
styles: ['/public/css/home.css']
}
然后
router.beforeEach((to, from, next) => {
if (to.styles && to.styles.length > 0) {
to.styles.forEach(val => import(val));
next();
} else {
next();
}
})
这个方法访问首页的时候样式会后加载,会出现一下没样式,其他页面没问题,不知道为什么