最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue框架render方法如何替換template

 更新時間:2022年04月12日 09:26:15   作者:普通網(wǎng)友  
這篇文章主要介紹了vue框架render方法如何替換template,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

render方法替換template

使用template屬性創(chuàng)建組件模板

import Vue from 'vue'
?
const Item = Vue.component('Item', {
? template: `<div>
? ? ? ? ? ? ? ? <h2>子組件</h2>
? ? ? ? ? ? ? ? <slot></slot>
? ? ? ? ? ? ? </div>`
})
const app = new Vue({
? el: '#app',
? template: `<div ref="myDiv"> ? ? ? ? ? ? ?<item ref="item">
? ? ? ? ? ? ? ? <p ref="p">this is a slot</p>
? ? ? ? ? ? ? </item>
? ? ? ? ? ? </div>`,
?data: {
? ? count: 0 ?},
? components: {
? ? Item
? }
})

把父組件的template創(chuàng)建換成使用render方法創(chuàng)建

const app = new Vue({
? el: '#app',
? data: {
? ? count: 0
? },
? render (createElement) {
? ? return createElement(
? ? ? 'div', {
? ? ? ? ref: 'myDiv',
? ? ? ? // domProps: {
? ? ? ? // ? ?innerHTML: '<span>注意:添加該屬性會把后面的子節(jié)點覆蓋</span>'
? ? ? ? // },
? ? ? ? attrs: {
? ? ? ? ? ? id: 'test-id',
? ? ? ? ? ? title: 'test-title' ?
? ? ? ? }
? ? ? },
? ? ? [
? ? ? ? createElement('item', {
? ? ? ? ? ref: 'item'
? ? ? ? },
? ? ? ? [
? ? ? ? ? createElement('p', {
? ? ? ? ? ? ref: 'p'
? ? ? ? ? }, 'this is a slot')
? ? ? ? ])
? ? ? ])
? },
? components: {
? ? Item
? }
})

1.如上面更改后的代碼,render方法內(nèi)傳入createElement函數(shù),接下來使用createElement函數(shù)來創(chuàng)建節(jié)點。

2.函數(shù)方法格式 createElement('節(jié)點或組件名稱', {節(jié)點屬性}, [子節(jié)點])

3.先創(chuàng)建一個div元素, 內(nèi)部包含ref='myDiv'的屬性, 使用數(shù)組存放其子節(jié)點

4.數(shù)組內(nèi)子節(jié)點是 item組件, 屬性是 ref="item", 其子節(jié)點為p, 依次類推逐層創(chuàng)建子節(jié)點, 最后的文本節(jié)點使用字符串或變量即可,無需再用[]包含。

template和render用法對比

App.vue(主入口文件)

<template>
? ? <ParentCmp />
</template>
<script>
import ParentCmp from './ParentCmp';
export default {
? ? components: {
? ? ? ? ParentCmp
? ? },
}
</script>

ParentCmp.vue (template寫法)

<template>
? ? <div>
? ? ? ? <h1>我是parent組件</h1>
? ? ? ? <hr />
? ? ? ? <User style="background: #ccc" text="我是傳入的文本">
? ? ? ? ? ? <template v-slot:header>
? ? ? ? ? ? ? ? <p>這是名字為header的slot</p>
? ? ? ? ? ? </template>
? ? ? ? ? ? <p>這是填充默認(rèn)slot數(shù)據(jù)</p>
? ? ? ? ? ? <template v-slot:footer>
? ? ? ? ? ? ? ? <p>這是名字為footer的slot</p>
? ? ? ? ? ? </template>
? ? ? ? ? ? <template v-slot:item="props">
? ? ? ? ? ? ? ? <p>名字為item的作用域插槽。顯示數(shù)據(jù){{props}}</p>
? ? ? ? ? ? </template>
? ? ? ? ? ? <template v-slot:list="props">
? ? ? ? ? ? ? ? <p>名字為list的作用域插槽。顯示數(shù)據(jù){{props}}</p>
? ? ? ? ? ? </template>
? ? ? ? </User>
? ? </div>
</template>
<script>
import User from './User'
export default {
? ? components: {
? ? ? ? User
? ? },
? ? props: {},
? ? data() {
? ? ? ? return {}
? ? },
? ? methods: {}
}
</script>

User.vue (template寫法)

<template>
? ? <div>
? ? ? ? <h4>{{text}}</h4>
? ? ? ? <slot name="header"></slot>
? ? ? ? <slot>默認(rèn)的user slot</slot>
? ? ? ? <slot name="footer"></slot>
? ? ? ? <slot name="item" v-bind="item">item作用域插槽,展示姓名 {{item.name}}</slot>
? ? ? ? <slot name="list" v-bind="{list}">list作用域插槽</slot>
? ? </div>
</template>
<script>
export default {
? ? props: {
? ? ? ? text: String
? ? },
? ? data() {
? ? ? ? return {
? ? ? ? ? ? item: {
? ? ? ? ? ? ? ? name: '張三',
? ? ? ? ? ? ? ? age: 28,
? ? ? ? ? ? ? ? works: '前端、后端、設(shè)計、產(chǎn)品'
? ? ? ? ? ? },
? ? ? ? ? ? list: ['a','b','c']
? ? ? ? }
? ? }
}
</script>

ParentCmp.js (render寫法)

import User from './User'
export default {
? ? props: {},
? ? data() {
? ? ? ? return {}
? ? },
? ? methods: {},
? ? render(h) {
? ? ? ? return h('div',[
? ? ? ? ? ? h('h1', '我是parent組件'),
? ? ? ? ? ? h('hr'),
? ? ? ? ? ? h(User, {
? ? ? ? ? ? ? ? props: {
? ? ? ? ? ? ? ? ? ? text: '我是傳入的文本'
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? style: {
? ? ? ? ? ? ? ? ? ? background: '#ccc'
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? // 作用域插槽寫在scopedSlots里
? ? ? ? ? ? ? ? scopedSlots: {
? ? ? ? ? ? ? ? ? ? item: props => h('p', `名字為item的作用域插槽。顯示數(shù)據(jù)${JSON.stringify(props)}`),
? ? ? ? ? ? ? ? ? ? list: props => h('p', `名字為list的作用域插槽。顯示數(shù)據(jù)${JSON.stringify(props)}`)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },?
? ? ? ? ? ? // 非作用域插槽寫數(shù)組里
? ? ? ? ? ? [
? ? ? ? ? ? ? ? h('p', {slot: 'header'}, '這是名字為header的slot'),
? ? ? ? ? ? ? ? h('p', '這是填充默認(rèn)slot數(shù)據(jù)'),
? ? ? ? ? ? ? ? h('p', {slot: 'footer'}, '這是名字為footer的slot'),
? ? ? ? ? ? ])
? ? ? ? ]);
? ? ? ? // jxs寫法
? ? ? ? /* return (
? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? <h1>我是parent組件</h1>
? ? ? ? ? ? ? ? <hr />
? ? ? ? ? ? ? ? <User?
? ? ? ? ? ? ? ? ? ? style="background: #ccc"?
? ? ? ? ? ? ? ? ? ? text="我是傳入的文本"?
? ? ? ? ? ? ? ? ? ? scopedSlots={
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? item: props => (<p>名字為item的作用域插槽。顯示數(shù)據(jù){JSON.stringify(props)}</p>),
? ? ? ? ? ? ? ? ? ? ? ? ? ? list: props => (<p>名字為list的作用域插槽。顯示數(shù)據(jù){JSON.stringify(props)}</p>),
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? >
? ? ? ? ? ? ? ? ? ? <p slot="header">這是名字為header的slot</p>
? ? ? ? ? ? ? ? ? ? <p>這是填充默認(rèn)slot數(shù)據(jù)</p>
? ? ? ? ? ? ? ? ? ? <p slot="footer">這是名字為footer的slot</p>
? ? ? ? ? ? ? ? </User>
? ? ? ? ? ? </div>
? ? ? ? ); */
? ? }
}

User.js (render寫法)

export default {
? ? props: {
? ? ? ? text: String
? ? },
? ? data () {
? ? ? ? return {
? ? ? ? ? ? item: {
? ? ? ? ? ? ? ? name: '張三',
? ? ? ? ? ? ? ? age: 28,
? ? ? ? ? ? ? ? works: '前端、后端、設(shè)計、產(chǎn)品'
? ? ? ? ? ? },
? ? ? ? ? ? list: ['a', 'b', 'c']
? ? ? ? }
? ? },
? ? methods: {
? ? ? ? getSlot (name, data) {
? ? ? ? ? ? if (this.$scopedSlots[name]) {
? ? ? ? ? ? ? ? return this.$scopedSlots[name](data);
? ? ? ? ? ? } else if (this.$slots[name]) {
? ? ? ? ? ? ? ? return this.$slots[name];
? ? ? ? ? ? }
? ??
? ? ? ? ? ? return undefined;
? ? ? ? },
? ? },
? ? render (h) {
? ? ? ? return h('div', [
? ? ? ? ? ? h('h4', this.text),
? ? ? ? ? ? this.getSlot('header'),
? ? ? ? ? ? this.$slots.default,
? ? ? ? ? ? this.getSlot('footer'),
? ? ? ? ? ? this.getSlot('item', this.item),
? ? ? ? ? ? this.getSlot('list', {list: this.list}),
? ? ? ? ])
? ? ? ? // jxs寫法
? ? ? ? /* return (
? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? <h4>{this.text}</h4>
? ? ? ? ? ? ? ? {this.getSlot('header')}
? ? ? ? ? ? ? ? {this.$slots.default}
? ? ? ? ? ? ? ? {this.getSlot('footer')}
? ? ? ? ? ? ? ? {this.getSlot('item', this.item)}
? ? ? ? ? ? ? ? {this.getSlot('list', {list: this.list})}
? ? ? ? ? ? </div>
? ? ? ? ); */
? ? }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • 詳解vue項目打包步驟

    詳解vue項目打包步驟

    這篇文章主要介紹了vue項目打包步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案

    微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案

    今天小編就為大家分享一篇微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Element el-table 表格使用詳解

    Element el-table 表格使用詳解

    我們的數(shù)據(jù)存儲到數(shù)據(jù)庫,不就是以表格的形式存在嗎,所以在界面上顯示、操作,使用表格來處理也是非常合理的,這篇文章給大家介紹Element el-table 表格使用方法,感興趣的朋友一起看看吧
    2024-03-03
  • vue Antd 輸入框Input自動聚焦方式

    vue Antd 輸入框Input自動聚焦方式

    這篇文章主要介紹了vue Antd 輸入框Input自動聚焦方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 淺談Vue.js應(yīng)用的四種AJAX請求數(shù)據(jù)模式

    淺談Vue.js應(yīng)用的四種AJAX請求數(shù)據(jù)模式

    本篇文章主要介紹了淺談Vue.js應(yīng)用的四種AJAX請求數(shù)據(jù)模式,本文將詳細(xì)介紹在Vue應(yīng)用程序中實現(xiàn)AJAX的四個方法,有興趣的可以了解一下
    2017-08-08
  • Vue實現(xiàn)下拉加載更多

    Vue實現(xiàn)下拉加載更多

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)下拉加載更多,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Vue時間軸 vue-light-timeline的用法說明

    Vue時間軸 vue-light-timeline的用法說明

    這篇文章主要介紹了Vue時間軸 vue-light-timeline的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Vue中provide、inject詳解以及使用教程

    Vue中provide、inject詳解以及使用教程

    provide和inject主要為高階插件/組件庫提供用例,并不推薦直接用于應(yīng)用程序代碼中,下面這篇文章主要給大家介紹了關(guān)于Vue中provide、inject詳解以及使用的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 使用Vue3和ApexCharts實現(xiàn)交互式3D折線圖

    使用Vue3和ApexCharts實現(xiàn)交互式3D折線圖

    ApexCharts 是一個功能強大的 JavaScript 庫,用于創(chuàng)建交互式、可定制的圖表,在 Vue.js 中,它可以通過 vue3-apexcharts 插件輕松集成,本文給大家介紹了使用Vue3和ApexCharts實現(xiàn)交互式3D折線圖,需要的朋友可以參考下
    2024-06-06
  • 在Vue3項目中使用Vuex進(jìn)行狀態(tài)管理的詳細(xì)教程

    在Vue3項目中使用Vuex進(jìn)行狀態(tài)管理的詳細(xì)教程

    在?Vue?3?中使用?Vuex?進(jìn)行狀態(tài)管理是一個很好的實踐,特別是在涉及到多個組件間共享狀態(tài)的情況,下面是如何在?Vue?3?項目中設(shè)置和使用?Vuex?的教程,包括?state,?mutations,?actions,?getters?的概念及其用途,需要的朋友可以參考下
    2024-09-09

最新評論

德兴市| 磐安县| 久治县| 平潭县| 临城县| 山东省| 临泽县| 汝城县| 柞水县| 荣成市| 开化县| 永康市| 巴彦县| 泸定县| 安顺市| 兴业县| 灵川县| 革吉县| 金阳县| 浏阳市| 兰考县| 突泉县| 东城区| 旬阳县| 同德县| 宁蒗| 陈巴尔虎旗| 深泽县| 通州区| 新源县| 乌审旗| 名山县| 扎赉特旗| 财经| 资兴市| 迭部县| 美姑县| 高碑店市| 惠来县| 民县| 北票市|