微信小程序import和include
生活随笔
收集整理的這篇文章主要介紹了
微信小程序import和include
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import 和include 是微信小程序提供的2中引用方式
import 一般結合模板template使用
import
import可以在該文件中使用目標文件定義的template,如:
在 item.wxml 中定義了一個叫item的template:
<!-- item.wxml -->
<template name="item"><text>{{text}}</text>
</template>
在 index.wxml 中引用了 item.wxml,就可以使用item模板:
<import src="item.wxml" />
<template is="item" data="{{text: 'forbar'}}" />
?
import 的作用域
import 有作用域的概念,即只會 import 目標文件中定義的 template,而不會 import 目標文件 import 的 template。
如:C import B,B import A,在C中可以使用B定義的template,在B中可以使用A定義的template,但是C不能使用A定義的template。
<!-- A.wxml -->
<template name="A"><text>A template</text>
</template>
<!-- B.wxml -->
<import src="a.wxml" />
<template name="B"><text>B template</text>
</template>
<!-- C.wxml -->
<import src="b.wxml" />
<template is="A" />
<!-- Error! Can not use tempalte when not import A. -->
<template is="B" />
以上內容來自官網,
include?
include?可以將目標文件除了?<template/>?<wxs/>?外的整個代碼引入,相當于是拷貝到?include?位置
感覺和android里面的include 里面的類似,使用的時候直接在wxml里面插入即可
例如
<include src="header.wxml" />
我寫了一個demo僅供參考
demo地址如果幫助了你,希望給個免費的star
?
總結
以上是生活随笔為你收集整理的微信小程序import和include的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个不在乎个性签名
- 下一篇: 微信小程序模板template