微信小程序

微信小程序的组件及分包

2020-05-10  本文已影响0人  南崽

自定义组件

定义组件

"component":true,

在页面中使用组件

"usingComponents":{
"组件的名称":"组件的地址"
}
<组件的名称></组件的名称>
<item></item>

组件的参数传递

properties:{
title:{type:"string",value:""}
}
triggerEvent("事件名",事件参数)
<item bind事件="响应函数"></item>
<item bindclick="clickHd">
clickHd(e){
e.detail 事件参数
}

组件的css

stylesolation:"apply-shared"
// isolated 表示启用样式隔离,
// apply-shared 表示页面 wxss 样式将影响到自定义组件,
// shared 表示页面 wxss 样式将影响到自定义组件

1). 组件的js定义

externalClasses:["item-class"]
<view class="item-class"></view>
<item item-class="myitem"></item>
.myitem{color:red}

组件的插槽

<item><text>插槽内容</text></item>
<view class="item item-class">
<slot></slot>
</view>
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: { /* ... */ },
  methods: { /* ... */ }
})
<item><icon type="info" slot="icon"></icon></item>
<view class="wrapper">
  <slot name="before"></slot>
  <view>这里是组件的内部细节</view>
  <slot name="after"></slot>
</view>

组件的生命周期

created 被创建
attached 组件实例进入页面节点
ready 组件视图渲染完毕
moved 组件被移动
detached 组件从页面中移除
error 组件被移除

组件内可使用的页面生命周期

  • pageLifetimes
  • show
  • hide
  • resize(size) 尺寸

分包

why

1.把小程序分为多个包,先下载主包,有空闲再去下载分包

分包原则

步骤

"subpackages": [
    {
      "root": "moduleA",
      "pages": [
        "pages/rabbit",
        "pages/squirrel"
      ]
    }
  ]
}

预加载

"preloadRule":{
"pages/index": {
      "network": "all",
      "packages": ["important"]
    },
    "sub1/index": {
      "packages": ["hello", "sub3"]
    },
}
//当进入到首页 去加载yidian这个分包
//当进入到jok 去加载base这个分包

npm安装插件

1. 右上角 详情>使用npm模板

2. 进入cmd

进入项目文件

npm init 初始化项目

3.

npm i @vant/weapp -S --production

4. 菜单-->工具-->构建npm

5. 在页面的json配置中引用

"usingComponents":{
"van-button":"@vant/weapp/button"
}

6. 在页面wxml里面使用

<van-button type="primary">按钮</van-button>

小程序的登录流程

用户头像信息

wx.getSetting(Object object)

获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限

wx.getUserInfo(Object object)

获取用户信息

上一篇 下一篇

猜你喜欢

热点阅读