【Vue 从入门到实战 进阶式掌握完整知识体系】011
【Vue 从入门到实战 进阶式掌握完整知识体系】011
2、组件间传值及传值校验父组件给子组件传值代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta ht
【Vue 从入门到实战 进阶式掌握完整知识体系】011
2、组件间传值及传值校验
父组件给子组件传值
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
template: `
<div>
<test message="Hello World!" />
</div>
`
});
appponent('test',{
props: ['message'],
template: `
<div>{{message}}</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
父组件给子组件传动态参数
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
data(){
return{
num: 12
}
},
template: `
<div>
<test message="12" />
<test :message="num" />
</div>
`
});
appponent('test',{
props: ['message'],
template: `
<div>{{ typeof message }}</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
子组件校验父组件的传参
代码语言:javascript代码运行次数:0运行复制String、Array、Boolean、Object、Function、Symbol
<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
data(){
return{
num: 12
}
},
template: `
<div>
<test message="12" />
<test :message="num" />
</div>
`
});
// String、Array、Boolean、Object、Function、Symbol
appponent('test',{
props: {
message: String
},
template: `
<div>{{ typeof message }}</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
父组件给子组件传函数
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
methods:{
add(){
cole.log("函数执行成功!")
}
},
template: `
<div>
<test :message="add" />
</div>
`
});
// 这里写成校验的写法
appponent('test',{
props: {
message: Function
},
template: `
<div>
{{ typeof message }}
<button @click="message">点我</button>
</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
设置必须传参数
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
template: `
<div>
<test message="hello" />
<!--不传参-->
<test />
</div>
`
});
// 这里写成校验的写法
appponent('test',{
props: {
message: {
type: String, // 要求传字符串(类型)
required: true // 要求必须传(是否必须)
}
},
template: `
<div>
{{ typeof message }}
{{ message }}
</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
为参数设置默认值
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
template: `
<div>
<test message="hello" />
<!--不传参-->
<test />
</div>
`
});
// 这里写成校验的写法
appponent('test',{
props: {
message: {
type: String, // 要求传字符串
required: true, // 要求必须传
default: '大哥刘备' // 默认值,也可以是一个函数
}
},
template: `
<div>
{{ typeof message }}
{{ message }}
</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
要求值小于1000
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<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">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
ct app = ({
data(){
return{
number: 2000
}
},
template: `
<div>
<test :message="number" />
</div>
`
});
// 这里写成校验的写法
appponent('test',{
props: {
message: {
type: umber, // 要求传数字
required: true, // 要求必须传
default: 100, // 默认值
validator: function(value){
return value < 1000;
} // 限定值
}
},
template: `
<div>
{{ typeof message }}
{{ message }}
</div>
`
})
ct vm = ('#root');
</script>
</html>
运行结果
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-23 18:17:29
上一篇:【Vue3 从入门到实战 进阶式掌握完整知识体系】012
下一篇:Java反射详解
推荐阅读
留言与评论(共有 7 条评论) |
本站网友 kangwei | 12分钟前 发表 |
{ props | |
本站网友 野田佳彦访华 | 12分钟前 发表 |
Symbol appponent('test' | |
本站网友 挑挑拣拣 | 19分钟前 发表 |
` <div> {{ typeof message }} <button @click="message">点我</button> </div> ` }) ct vm = ('#root'); </script> </html>运行结果image-2021061111125225.png设置必须传参数代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width | |
本站网友 郑州团购房 | 8分钟前 发表 |
` <div>{{ typeof message }}</div> ` }) ct vm = ('#root'); </script> </html>运行结果image-2021061110404258.png子组件校验父组件的传参 String | |
本站网友 防辐射的植物 | 19分钟前 发表 |
{ props | |
本站网友 345 | 10分钟前 发表 |
原始发表:2021-06-22 |