- 场景
- 子组件的样式绑定
- 如何区分父子组件
- 子组件使用样式的小坑
- 行内样式的编写
这篇文章继续学习Vue的模板样式绑定。上篇文章你已经对Vue中的样式绑定有一个基本了解。我们预习一下,
上节我们学了三种绑定样式的方法:
通过普通字符串进行绑定; 通过对象的方式进行绑定; 通过数组的方式进行绑定。
这篇文章主要学习一下Vue中子组件样式的绑定和行内样式如何编写。
子组件的样式绑定app.component('sonCom',{
template:`SonCom`
})
有了子组件后,就可以在父组件的模板中进行使用了,使用就是直接写一个类似 html
的标签进去就可以。
template:`willem`
DOCTYPE html>
demo
.red{color:red !important;}
.green{color:green;}
.background{ background-color: orange;}
const app = Vue.createApp({
data() {
return {
classString: 'red',
classObject:{red:true,background:true},
classArray:['green','background',{red:true}],
}
},
template: ` hello `
});
app.component('sonCom',{
template:`SonCom`
});
const vm = app.mount("#app");
如何区分父子组件
在 vue.createApp()
方法中用对象形式 { }
配置的一般叫做父组件,而下面使用的其他组件,叫做子组件。你也可以这样理解,主动调用的是父组件,被调用的是子组件。
最简单的为子组件添加样式的方法,就是自己给子组件加上class。
app.component('sonCom',{
template:`SonCom`
})
这时候子组件的字体颜色就变成了绿色。你还可以把class写在调用子组件的地方(也就是写在父组件里),
例如下面的代码:
template:`willem`
DOCTYPE html>
demo
.red{color:red !important;}
.green{color:green;}
.background{ background-color: orange;}
const app = Vue.createApp({
data() {
return {
classString: 'red',
classObject:{red:true,background:true},
classArray:['green','background',{red:true}],
}
},
template:`willem`
});
app.component('sonCom',{
template:`SonCom`
});
const vm = app.mount("#app");
先去掉子组件里的class,在调用地方增加class样式。
这时候效果也是一样的。
子组件使用样式的小坑这时候我们修改一下子组件,再写一个 这时候再来看结果。 你会发现两个 那我们如何让它变成绿色那,其实只有再两个并列的 也就是说让子组件的最外层只有一个根元素。 这样就又变成了绿色字体。 还有一种用到动态绑定的方法,直接绑定属性中的class。 什么是行内样式 ? 就是自己在模板的DOM元素上写CSS样式, 比如下面的这样: 除了这种写法以外,Vue中也为我们扩展了一些内容,让行内样式的写法更直观和另类。你可以直接在data中编写样式, 比如在 然后用绑定行内样式的形式,在模板中进行绑定。 你也可以用对象的形式在data中编写CSS样式。 比如写成下面的代码,然后再进行绑定。 在写行内样式的使用,个人觉的对象的写法更加直观和简洁,所以建议小伙伴可以采用这种对象的形式来进行编写。app.component('sonCom',{
template:`
SonCom
willem笔记
`
})
app.component('sonCom',{
template:`
SonCom
willem笔记
`
})
DOCTYPE html>
demo
.red{color:red !important;}
.green{color:green;}
.background{ background-color: orange;}
const app = Vue.createApp({
data() {
return {
classString: 'red',
classObject:{red:true,background:true},
classArray:['green','background',{red:true}],
}
},
template:`willem`
});
app.component('sonCom',{
template:`
app.component('sonCom',{
template:`
SonCom
willem笔记
`
})
行内样式的编写
DOCTYPE html>
demo
.red{color:red !important;}
.green{color:green;}
.background{ background-color: orange;}
const app = Vue.createApp({
data() {
return {
classString: 'red',
classObject:{red:true,background:true},
classArray:['green','background',{red:true}],
}
},
template:`willem`
});
app.component('sonCom',{
template:`
willem
Data
中这样写:data(){
return{
styleString:'color:orange;'
}
},
template:`
willem
`
DOCTYPE html>
demo
.red{color:red !important;}
.green{color:green;}
.background{ background-color: orange;}
const app = Vue.createApp({
data(){
return{
styleString:'color:orange;'
}
},
template:`
willem
`
});
const vm = app.mount("#app");
data(){
return{
styleString:'color:orange;',
styleObject:{
color:'red',
background:'yellow'
}
}
},
DOCTYPE html>
demo
.red{color:red !important;}
.green{color:green;}
.background{ background-color: orange;}
const app = Vue.createApp({
data(){
return{
styleString:'color:orange;',
styleObject:{
color:'red',
background:'yellow'
}
}
},
template:`
willem
`
});
const vm = app.mount("#app");
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?


微信扫码登录