https://blog.csdn.net/smartsmile2012/article/details/83414642
================小程序引用外部js======================
//封裝的函數
function GetUserInfo2018() {
console.log("獲取用戶信息8888")
}
function count(str) {
console.log(str)
}
//轉化成小程序模板語言 這一步非常重要 不然無法正確調用
module.exports = {
GetUserInfo2018: GetUserInfo2018,
count: count
};
/*其它頁面調用
var common = require("../common/common.js");
common.GetUserInfo2018();
common.count("hehe");
*/
==============小程序引用外部css=========================
/*
app.wxss是全局樣式,作用於每一個頁面,
而page下的每一個的wxss文件只作用於當前頁面,並對全局樣式中的相同屬性會覆蓋
*/
@import "../../app.wxss";
/**index.wxss**/
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}
.userinfo-avatar {
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 20%;
}
.userinfo-nickname {
color: #aaa;
}
.usermotto {
margin-top: 200px;
}
============小程序引用公共頁面===================
1、不帶參數
首先在pages文件夾中新建一個template文件夾,文件夾中新建一個template.wxml文件,代碼如下
<!--template.wxml-->
<template name="msgItem">
<view>
<text>This is template.wxml文件,我是一個模板</text>
</view>
</template>
然后我們書寫我們所要調用template的頁面index.wxml
<!--index.wxml-->
<!-- 聲明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<template is="msgItem"/>
2、帶參數
首先,修改template.wxml文件,我們給模板添加三個字段,修改后代碼如下
復制代碼
<template name="msgItem">
<view>
<text>This is template.wxml文件,我是一個模板</text>
<view>
<text> {{index}}: {{msg1}} </text>
<text> {{msg2}} </text>
</view>
</view>
</template>
復制代碼
接下來我們在index.wxml中傳遞模板中所需要的三個參數,修改后的代碼如下:
<!--index.wxml-->
<!-- 聲明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<template is="msgItem" data="{{index:1,msg1:'msg1數據',msg2:'msg2數據'}}"/>
3、列表item模板
接下來我們就通過一種常見的情況列表數據來使用模板,增加對模板的認知,直接上修改過的代碼:
復制代碼
//index.js
Page({
data: {
list:[
{ name: '張三', age: 15 },
{ name: '李四', age: 25 },
{ name: '王五', age: 18 },
{ name: '趙六', age: 19 },
]
}
})
復制代碼
復制代碼
<!--index.wxml-->
<!-- 聲明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<view wx:for="{{list}}">
<template is="msgItem" data="{{name:item.name,age:item.age}}"/>
</view>
復制代碼
復制代碼
<!--template.wxml-->
<template name="msgItem">
<view>
<text> name: {{name}} </text>
<text> age: {{age}}</text>
</view>
</template>
4、調用不同的模板
有時候,我們有這樣的需求,那就是同一個列表中,item數據不同,可能他的樣式也是有很大的區別,所以我們使用的模板也會對應不相同,接下來我們就來實現這樣需求的小Demo:
首先修改了一下template.wxml,原本該文件中只有一個template,現在我們創建了兩個,新增的template僅僅多了一行代碼,當然了實際開發中,需求會比這個難很多,在這里只是為了實現Demo。
復制代碼
<!--template.wxml-->
<template name="msgItem">
<view class="template_style">
<text> name: {{name}} </text>
<text class="template_age_style"> age: {{age}}</text>
</view>
</template>
<template name="msgItem2">
<view class="template_style">
<text> name: {{name}} </text>
<text class="template_age_style"> age: {{age}}</text>
<text>我是一個未成年</text>>
</view>
</template>
復制代碼
接下來我們在index.wxml中通過age字段調用不同的模板:
復制代碼
<!--index.wxml-->
<!-- 聲明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<view wx:for="{{list}}">
<template is="{{item.age >= 18 ? 'msgItem' : 'msgItem2'}}" data="{{name:item.name,age:item.age}}"/>
</view>
復制代碼
---------------------
作者:smartsmile2012
來源:CSDN
原文:https://blog.csdn.net/smartsmile2012/article/details/83414642
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!