11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
45 < meta charset ="UTF-8 ">
56 < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
67 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7- < title > Vue开关组件</ title >
8+ < title > 5. Vue开关组件</ title >
89 < script src ="https://unpkg.com/vue@next "> </ script >
910</ head >
11+
1012< body >
1113 < div id ="Application ">
12- < my-switch @switch-change ="change1 " switch-style ="mini " background-color ="green " border-color ="green " color ="blue "> </ my-switch >
14+ < my-switch @switch-change ="change1 " switch-style ="mini " background-color ="green " border-color ="green "
15+ color ="blue "> </ my-switch >
1316 < div > 开关状态:{{state1}}</ div >
14- < br />
15- < my-switch @switch-change ="change2 " switch-style ="normal " background-color ="blue " border-color ="blue " color ="red "> </ my-switch >
17+ < br />
18+ < my-switch @switch-change ="change2 " switch-style ="normal " background-color ="blue " border-color ="blue "
19+ color ="red "> </ my-switch >
1620 < div > 开关状态:{{state2}}</ div >
1721 </ div >
1822 < script >
1923 const App = Vue . createApp ( {
20- data ( ) {
24+ data ( ) {
2125 return {
22- state1 :"关" ,
23- state2 :"关"
26+ state1 : "关" ,
27+ state2 : "关"
2428 }
2529 } ,
26- methods :{
27- change1 ( isOpen ) {
30+ methods : {
31+ change1 ( isOpen ) {
2832 this . state1 = isOpen ? "开" : "关"
2933 } ,
30- change2 ( isOpen ) {
34+ change2 ( isOpen ) {
3135 this . state2 = isOpen ? "开" : "关"
3236 } ,
3337 }
3438 } )
3539
3640 const switchComponent = {
3741 // 定义的外部属性
38- props :[ "switchStyle" , "borderColor" , "backgroundColor" , "color" ] ,
42+ props : [ "switchStyle" , "borderColor" , "backgroundColor" , "color" ] ,
3943 // 内部属性,控制开关状态
4044 data ( ) {
4145 return {
42- isOpen :false ,
43- left :'0px'
46+ isOpen : false ,
47+ left : '0px'
4448 }
4549 } ,
4650 // 通过计算属性来设置CSS样式
4751 computed : {
48- cssStyleBG :{
52+ cssStyleBG : {
4953 get ( ) {
5054 if ( this . switchStyle == "mini" ) {
51- return `position: relative; border-color: ${ this . borderColor } ; border-width: 2px; border-style: solid;width:55px; height: 30px;border-radius: 30px; background-color: ${ this . isOpen ? this . backgroundColor : 'white' } ;`
55+ return `position: relative; border-color: ${ this . borderColor } ; border-width: 2px; border-style: solid;width:55px; height: 30px;border-radius: 30px; background-color: ${ this . isOpen ? this . backgroundColor : 'white' } ;`
5256 } else {
53- return `position: relative; border-color: ${ this . borderColor } ; border-width: 2px; border-style: solid;width:55px; height: 30px;border-radius: 10px; background-color: ${ this . isOpen ? this . backgroundColor : 'white' } ;`
57+ return `position: relative; border-color: ${ this . borderColor } ; border-width: 2px; border-style: solid;width:55px; height: 30px;border-radius: 10px; background-color: ${ this . isOpen ? this . backgroundColor : 'white' } ;`
5458 }
5559 }
5660 } ,
57- cssStyleBtn :{
61+ cssStyleBtn : {
5862 get ( ) {
5963 if ( this . switchStyle == "mini" ) {
6064 return `position: absolute; width: 30px; height: 30px; left:${ this . left } ; border-radius: 50%; background-color: ${ this . color } ;`
6973 click ( ) {
7074 this . isOpen = ! this . isOpen
7175 this . left = this . isOpen ? '25px' : '0px'
76+ // 自组件调用父组件
7277 this . $emit ( 'switchChange' , this . isOpen )
7378 }
7479 } ,
75- template :`
80+ template : `
7681 <div :style="cssStyleBG" @click="click">
7782 <div :style="cssStyleBtn"></div>
7883 </div>
8287 App . mount ( "#Application" )
8388 </ script >
8489</ body >
90+
8591</ html >
0 commit comments