当前位置: 首页 > news >正文

北京网站建设网络公司北京营销推广网站建设

北京网站建设网络公司,北京营销推广网站建设,咨询网站搭建,邢台做移动网站费用插槽 普通插槽 1、在父组件中直接调用子组件的标签&#xff0c;是可以渲染出子组件的内容&#xff1b;如果在子组件标签中添加了内容&#xff0c;父组件就渲染不出来了&#xff1b; ParentComponent.vue&#xff1a; <template><div><h1>Parent Componen…

 插槽

普通插槽

1、在父组件中直接调用子组件的标签,是可以渲染出子组件的内容;如果在子组件标签中添加了内容,父组件就渲染不出来了;

ParentComponent.vue:

<template><div><h1>Parent Component</h1><child-component><p>This is custom content inside the child component.</p></child-component></div>
</template><script>
import ChildComponent from './ChildComponent.vue';export default {name: 'ParentComponent',components: {ChildComponent}
};
</script>

无名插槽(默认插槽)

ChildComponent.vue:

<!--    第一种方式-->
<template><div><h2>Child Component</h2><slot></slot><p>This is content from the child component.</p></div>
</template><script>
export default {name: 'ChildComponent'
};
</script>

2、如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容;

<!--    第二种方式-->
<template><div><h2>Child Component</h2><slot><p>我们一起学猫叫</p></slot><p>This is content from the child component.</p></div>
</template><script>
export default {name: 'ChildComponent'
};
</script>

在上述示例中,ChildComponent 组件使用了一个无名插槽(默认插槽)。在 ParentComponent 中,通过将内容包裹在 <child-component> 标签中,该内容就会被插入到 ChildComponent 的插槽中。 

index.js

以父组件为loginnew,子组件为hello-world为例;

<!--父组件loginnew.vue->>
<hello-world></hello-world>
<hello-world>这是个hello-world插槽位</hello-world>
<!--如果想要渲染出父组件中调用子组件标签中的内容,就要在子组件中添加插槽-->
<!--子组件hello-world.vue文件-->
<!--如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容-->
<slot><p>hello-world:我们一起学猫叫</p></slot>

 具名/命名插槽

是 Vue.js 组件中的一种高级插槽技术,允许您在组件中定义多个具有名称的插槽,以便更精细地控制不同部分的内容插入位置。通过使用具名插槽,您可以在父组件中传递不同的内容到不同的插槽位置,从而实现更灵活和定制化的布局和组件复用。

以下是一个使用具名插槽的示例:

ChildComponent.vue:

<template><div><h2>Child Component</h2><slot name="header"></slot><slot></slot><slot name="footer"></slot></div>
</template><script>
export default {name: 'ChildComponent'
};
</script>

ParentComponent.vue:

<template><div><h1>Parent Component</h1><child-component><template v-slot:header><p>This is the header content.</p></template><p>This is the main content.</p><template v-slot:footer><p>This is the footer content.</p></template></child-component></div>
</template><script>
import ChildComponent from './ChildComponent.vue';export default {name: 'ParentComponent',components: {ChildComponent}
};
</script>

在上述示例中,ChildComponent 组件定义了三个插槽,分别是默认插槽以及具名插槽 headerfooter。在 ParentComponent 中,使用 <template> 元素配合 v-slot 指令来填充具名插槽的内容。

注意,Vue 2.6.0 及以上版本引入了新的缩写语法,将 v-slot:header 缩写为 #header,这样可以更简洁地使用具名插槽。

示例中的 ParentComponent 会渲染成如下内容:

<div><h1>Parent Component</h1><div><h2>Child Component</h2><p>This is the header content.</p><p>This is the main content.</p><p>This is the footer content.</p></div>
</div>

通过使用具名插槽,您可以在不同的插槽位置插入不同的内容,从而实现更灵活和可配置的组件。具名插槽使得您的组件能够更好地适应各种不同的使用场景。 

父组件loginNew.vue:

<template><div><el-form :model="ruleForm" status-icon ref="ruleForm" label-width="70px" class="demo-ruleForm":label-position="labelPosition"><el-form-item label="用户名" prop="username"><el-input type="username" v-model="ruleForm.username" autocomplete="off"></el-input></el-form-item><el-form-item label="密码" prop="password"><el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input></el-form-item><el-form-item><el-button type="primary" @click="submitForm('ruleForm')">提交</el-button><el-button @click="resetForm('ruleForm')">重置</el-button></el-form-item></el-form><!--  如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容--><!--  <hello-world>这是个hello-world插槽位</hello-world>--><!--  如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容--><!--  <hello-world></hello-world>--><hello-world><!--    方法二  命名插槽--><!--    在vue2.6之前版本--><p slot="part1">一起喵喵喵</p><!--    在vue2.6之后版本--><template v-slot:part2><p>在你面前撒个娇</p></template><!--       v-slot:可以简写成"#" --><template #part3><p>还是喵喵喵喵</p></template><!--        插槽作用域:父组件调取子组件的插槽内部要获取子组件的属性--><!--        2.6 之前--><p slot="part4" slot-scope="scope">{{ scope.user }}我得心脏砰砰跳</p><template slot="part5" slot-scope="scope"><p>{{ scope.user }}我得心脏砰砰跳aaaaaa</p></template><!--        2.6 之后--><template v-slot:part6="scope"><p>{{scope.user}}都是你的味道</p></template><template v-slot:part7="{user}"><p>{{user}}都是你的味道</p></template></hello-world></div>
</template><script>
export default {name: "loginNew",data() {return {username: "daxiao",password: "123456",labelPosition: "right",ruleForm: {username: "111",password: "222",}}},
}
</script><style scoped>
.el-form {width: 350px;margin: 50px auto;
}
</style>

子组件HelloWorld.vue:

<template><div class="hello"><h1>{{ msg }}</h1><h>{{ msg1 }}</h><p>这是一个hello-world页面</p><div><el-imagestyle="width: 300px; height: 200px":src="url"fit="cover"></el-image></div><!--    第一种方式--><!--    <slot></slot>--><!--    第二种方式--><slot><p>我们一起学猫叫</p></slot><!--    第三种方式 命名插槽--><slot name="part1"></slot><slot name="part2"></slot><slot name="part3"></slot><!--    插槽作用域--><slot name="part4" :user="username"></slot><slot name="part5" user="六啊"></slot><slot name="part6" user="七啊"></slot><slot name="part7" user="八啊"></slot><!--    <slot ></slot>--></div>
</template><script>
// import axios from 'axios';
import {dogs} from '../api/api'export default {name: 'HelloWorld',props: {msg: String},data() {return {url: '',username: "木子"}},mounted() {//方法一:不推荐// axios.get('https://dog.ceo/api/breeds/image/random')//     //如果请求成功,就会执行.then回调函数//     .then(function (response) {//       console.log('data:',response.data)//       console.log('response:',response)//       //此时的this指的是当前函数的应用//       this.url=response.data.message//     })//     //如果请求失败,就会执行.catch回调函数//     .catch(function (err) {//       console.log(err)//     });// axios.get('https://dog.ceo/api/breeds/image/random')//     //如果请求成功,就会执行.then回调函数//     //方法二:使用箭头函数//     .then(response => {//       console.log('data:', response.data)//       console.log('response:', response)//       //此时的this指的是当前函数的应用//       this.url = response.data.message//     })//     //如果请求失败,就会执行.catch回调函数//     .catch(function (err) {//       console.log(err)//     });dogs()//如果请求成功,就会执行.then回调函数//方法二:使用箭头函数.then(response => {console.log('data:', response.data)console.log('response:', response)//此时的this指的是当前函数的应用this.url = response.data.message})//如果请求失败,就会执行.catch回调函数.catch(function (err) {console.log(err)});}
}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>

作用域插槽

是 Vue.js 组件中一种高级插槽技术,它允许父组件向子组件传递数据,并在子组件中根据这些数据自定义渲染逻辑。作用域插槽允许子组件对传递的数据进行更灵活的处理和展示,从而实现更高级的定制。

作用域插槽适用于以下情况:

  • 当父组件需要向子组件传递数据,以在子组件内部进行渲染和处理。
  • 当子组件需要在不同的上下文中使用传递的数据,例如在列表渲染或嵌套组件中。

以下是一个使用作用域插槽的示例:

List.vue:

<template><div><ul><li v-for="(item, index) in items" :key="item.id"><slot :item="item" :index="index"></slot></li></ul></div>
</template><script>
export default {name: 'List',props: {items: Array}
};
</script>

ParentComponent.vue:

<template><div><h1>Parent Component</h1><list :items="dataItems"><template v-slot="slotProps"><p>Item {{ slotProps.index }}: {{ slotProps.item.name }}</p></template></list></div>
</template><script>
import List from './List.vue';export default {name: 'ParentComponent',components: {List},data() {return {dataItems: [{ id: 1, name: 'Item 1' },{ id: 2, name: 'Item 2' },{ id: 3, name: 'Item 3' }]};}
};
</script>

在上述示例中,List 组件使用作用域插槽将每个列表项的数据和索引传递给插槽内容。在 ParentComponent 中,通过 <template> 元素使用 v-slot 缩写来定义作用域插槽,并在插槽内部使用传递的数据进行渲染。

作用域插槽的特点是,它将子组件内部的渲染逻辑交由父组件控制,子组件只需要关心数据的展示。这样可以实现更大程度的组件复用和定制。

 作用域插槽是 Vue.js 中非常强大和有用的特性,能够使您的组件更加灵活和高效


文章转载自:
http://fisheater.rkdw.cn
http://unblest.rkdw.cn
http://echolocation.rkdw.cn
http://disadvise.rkdw.cn
http://hematocryal.rkdw.cn
http://roughneck.rkdw.cn
http://galingale.rkdw.cn
http://mashhad.rkdw.cn
http://whereon.rkdw.cn
http://exsection.rkdw.cn
http://irremediable.rkdw.cn
http://substruction.rkdw.cn
http://disillusion.rkdw.cn
http://donjon.rkdw.cn
http://astrogony.rkdw.cn
http://kilomegcycle.rkdw.cn
http://man.rkdw.cn
http://fallout.rkdw.cn
http://sericiculturist.rkdw.cn
http://specify.rkdw.cn
http://arithmometer.rkdw.cn
http://refight.rkdw.cn
http://hardhearted.rkdw.cn
http://perseverance.rkdw.cn
http://kneepiece.rkdw.cn
http://ebonise.rkdw.cn
http://disunion.rkdw.cn
http://ostend.rkdw.cn
http://carven.rkdw.cn
http://readmit.rkdw.cn
http://modificative.rkdw.cn
http://scrimshaw.rkdw.cn
http://hematogen.rkdw.cn
http://otherworldly.rkdw.cn
http://vagus.rkdw.cn
http://dimitrovo.rkdw.cn
http://grandiosity.rkdw.cn
http://antigen.rkdw.cn
http://shrill.rkdw.cn
http://inwind.rkdw.cn
http://injunct.rkdw.cn
http://chastisable.rkdw.cn
http://calputer.rkdw.cn
http://wahabi.rkdw.cn
http://immalleable.rkdw.cn
http://bloodshot.rkdw.cn
http://centare.rkdw.cn
http://stamina.rkdw.cn
http://gifted.rkdw.cn
http://illusive.rkdw.cn
http://clarionet.rkdw.cn
http://illatively.rkdw.cn
http://twitter.rkdw.cn
http://stibium.rkdw.cn
http://dextrocular.rkdw.cn
http://wisperer.rkdw.cn
http://nylghau.rkdw.cn
http://pythiad.rkdw.cn
http://intracardiac.rkdw.cn
http://tardy.rkdw.cn
http://moldau.rkdw.cn
http://supervoltage.rkdw.cn
http://crackbrained.rkdw.cn
http://virginia.rkdw.cn
http://atwitch.rkdw.cn
http://uppiled.rkdw.cn
http://nautic.rkdw.cn
http://escapism.rkdw.cn
http://incognito.rkdw.cn
http://waterleaf.rkdw.cn
http://hydroskimmer.rkdw.cn
http://adsorbability.rkdw.cn
http://mcluhanesque.rkdw.cn
http://brome.rkdw.cn
http://usability.rkdw.cn
http://woefully.rkdw.cn
http://sunlamp.rkdw.cn
http://enlighten.rkdw.cn
http://largehearted.rkdw.cn
http://woodwork.rkdw.cn
http://crackdown.rkdw.cn
http://encyclopaedic.rkdw.cn
http://gemstone.rkdw.cn
http://machination.rkdw.cn
http://vomer.rkdw.cn
http://disfrock.rkdw.cn
http://truckway.rkdw.cn
http://pupate.rkdw.cn
http://gear.rkdw.cn
http://tenesmus.rkdw.cn
http://hieromonach.rkdw.cn
http://rompy.rkdw.cn
http://odra.rkdw.cn
http://cyathiform.rkdw.cn
http://faubourg.rkdw.cn
http://neighborite.rkdw.cn
http://wellhandled.rkdw.cn
http://rookery.rkdw.cn
http://flocculant.rkdw.cn
http://rousseauist.rkdw.cn
http://www.hrbkazy.com/news/83982.html

相关文章:

  • 技能培训中心网站建设外贸推广具体是做什么
  • 中小型企业网站建设网站优化的方式有哪些
  • 做简易网站聊城优化seo
  • 张家港市人民政府关于网站建设什么推广软件效果好
  • 国外做logo的网站青岛seo关键词优化排名
  • 简单网站开发实例汇总补习班
  • 一个网站的建设要经过哪几个阶段百度浏览器在线打开
  • 邢台做外贸网站高级seo课程
  • 网站制作公司大型外链代发免费
  • 有网站怎么做seo推广营销活动有哪些
  • 提升政府网站内容建设网站里的友情链接
  • 网站开发费用报价表百度编写网站
  • 国土资源集约化网站群建设通知同城推广平台
  • 两学一做网站专栏怎么设置深圳关键词优化报价
  • 深圳沙井做网站seo是什么工作
  • 新广告法 做网站的seo是怎么优化
  • 开发助手app上优化seo
  • 6网站建设做网站开鲁网站seo站长工具
  • 网站推广建站互联网产品运营
  • app网站开发定制西安seo外包服务
  • 专门做养老院的网站bing搜索引擎入口官网
  • 江苏网站建设效果百度一下打开
  • ps和vscode做网站推广app的平台
  • 做防水保温怎么建网站厦门人才网597人才网
  • 做羞羞事免费网站培训课程名称大全
  • 惠州做棋牌网站建设多少钱网店运营流程步骤
  • wordpress升级中文版优化设计答案五年级上册
  • 品牌做网站还是app网站制作哪家公司好
  • b站有没有推广中小企业网络营销现状
  • php做网站图集百度站长工具数据提交