首页 > 热点 > 微信 > 正文

微信小程序如何引用外部js,外部样式,公共页面模板

2024-07-22 01:17:56
字体:
来源:转载
供稿:网友

小程序引用外部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.jsPage({ 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>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表