
Github项目地址 https://github.com/JofunLiang/vue-project-themable-demo
演示地址 https://jofunliang.github.io/vue-project-themable-demo/
可行性测试
为了检验方法的可行性,在public文件夹下新建一个themes文件夹,并在themes文件夹新建一个default.css文件:
:root { --color: red;}推荐学习:CSS视频教程
在public文件夹的index.html文件中引入外部样式theme.css,如下:
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title>vue-skin-peeler-demo</title> <!-- 引入themes文件夹下的default.css --> <link rel="stylesheet" type="text/css" href="src/themes/default.css" rel="external nofollow"> </head> <body> <noscript> <strong>We're sorry but vue-skin-peeler-demo doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <!-- built files will be auto injected --> </body></html>
然后,在Home.vue中使用CSS变量:
<template> <div> <div :class="$style.demo">变红色</div> </div></template><script>export default { name: 'home'}</script><style module> .demo { color: var(--color); }</style>然后,运行项目并在浏览器中打开页面,页面显示效果正常。
注意:@vue/cli使用link标签引入css样式可能报错“We're sorry but vue-skin-peeler-demo doesn't work properly without JavaScript enabled. Please enable it to continue.”。这是因为@vue/cli将src目录下的文件都通过webpack打包所引起,所以,静态文件资源要放在public(如果是@vue/cli 2.x版本放在static)文件夹下。










