vue2.x(scoped-style)

vue中如何做到局部style不污染全局样式?说说它的原理?

① 在一般的开发过程中 我们使用scoped来标识当前组件的局部样式

② scoped属性是HTML5中的新属性 属性值是一个布尔属性 若使用该属性 则样式仅仅应用到style元素的父元素及其子元素 但是目前就只有firefox支持这个特性 其他浏览器都不支持

<div>
    <style type="text/css" scoped>
        h1{color:red}
        p{color:blue}
    </style>
    <h1>我是红色字体h1</h1>
    <p>我是蓝色字体p</p>
</div>
<div>
    <h1>我是默认颜色h1</h1>
    <p>我是默认颜色p</p>
</div>

③ 在vue中使用原理:

其实这个属性一般我们在使用单文件.vue组件进行开发时使用 也就是说下面这种模式
<template></template>
<script><script>
<style lang='scss' scoped></style>
那就说明其实不是vue给我们作了scoped局部样式的处理 而是插件"vue-loader"作的处理

源码vue-loader/lib/loaders/stylePostLoader.js中 我们可以到最终渲染在dom上的自定义属性data-v-xxx 由vue-loader操作生成scopeId最后在vue源码中使用

同理style中的lang属性 来指定我们使用什么预处理css 如:scss sass less stylus等等均在vue-loader中处理lang字段最后在动态require引入指定的预处理css loader插件

④ 深度作用选择器:

通常情况下:
<style scoped>
.a >>> .b {
    color:red;
}
</style>
若使用类似于sass这类预处理无法解析>>>
<style scoped>
// scss
/deep/ .a .b{
    color:blue;
} 
// sass
.a .b::v-deep{
    color:yellow;
}
</style>

⑤ 除了scoped生成局部样式还有什么方式来实现样式作用域?

vue-loader还提供了css modules模式来进行设置局部样式 具体可以看配置项。

⑥ 关于热重载

最关键的就是状态保留:(配合webpack插件webpack-dev-server --hot使用热更新)

​ 编辑组件<template> — 组件实例就地重新渲染 — 模板被编译成了新的无副作用的render函数

​ 编辑组件<script> — 实例就地销毁且重新创建(组件状态保留是因为script中包含带有副作用生命钩子,若关联了全局副作用则整个页面重新reload)

​ 编辑组件<style> — 会通过vue-style-loader自行热加载 不影响组件状态
image-20210112103257270.png
image-20210112103540452.png

vue中watch的属性或者methods的方法能使用箭头函数来定义吗?为什么?

不能 因为不管在watch或methods的方法中我们都想以vue的实例来操作 若一旦我们使用箭头函数 则在非严格模式下 this绑定的是父级作用域window 而在严格模式下this是undefined

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Vue-cli 快速搭建基于 Vue.js 的项目结构并在此基础上添加 element-ui 和 three.js 的依赖库。以下是相关的命令: 1. 安装 Vue-cli ``` npm install -g @vue/cli ``` 2. 创建一个基于 Vue.js 的项目 ``` vue create your-project-name ``` 3. 进入项目目录并添加 element-ui 和 three.js 的依赖库 ``` cd your-project-name npm i element-ui three ``` 4. 在 main.js 中导入 element-ui 和 three.js 的样式和组件 ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import * as THREE from 'three' import App from './App.vue' Vue.config.productionTip = false Vue.use(ElementUI) Vue.prototype.$THREE = THREE new Vue({ render: h => h(App), }).$mount('#app') ``` 5. 在 App.vue 中创建一个包含 three.js 场景的组件 ```vue <template> <div class="three-container"></div> </template> <script> export default { name: 'App', mounted () { // 初始化 three.js 场景 const scene = new this.$THREE.Scene() const camera = new this.$THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ) const renderer = new this.$THREE.WebGLRenderer() renderer.setSize(window.innerWidth, window.innerHeight) document.querySelector('.three-container').appendChild(renderer.domElement) const geometry = new this.$THREE.BoxGeometry(1, 1, 1) const material = new this.$THREE.MeshBasicMaterial({ color: 0x00ff00 }) const cube = new this.$THREE.Mesh(geometry, material) scene.add(cube) camera.position.z = 5 const animate = () => { requestAnimationFrame(animate) cube.rotation.x += 0.01 cube.rotation.y += 0.01 renderer.render(scene, camera) } animate() } } </script> <style scoped> .three-container { width: 100%; height: 100%; position: fixed; } </style> ``` 这样就可以创建一个基于 Vue.js、element-ui 和 three.js 的后台管理系统模板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值