知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >
kbone 高級(jí) - 使用小程序內(nèi)置組件(一)
發(fā)表時(shí)間:2021-1-5
發(fā)布人:葵宇科技
瀏覽次數(shù):48
需要明確的是,如果沒有特殊需求的話,請(qǐng)盡量使用 html 標(biāo)簽來編寫代碼,使用內(nèi)置組件時(shí)請(qǐng)按需使用。這是因?yàn)榻^大部分內(nèi)置組件外層都會(huì)被包裹一層自定義組件,如果自定義組件的實(shí)例數(shù)量達(dá)到一定量級(jí)的話,理論上是會(huì)對(duì)性能造成一定程度的影響,所以對(duì)于 view、text、image 等會(huì)被頻繁使用的內(nèi)置組件,如果沒有特殊需求的話請(qǐng)直接使用 div、span、img 等 html 標(biāo)簽替代。
部分內(nèi)置組件可以直接使用 html 標(biāo)簽替代,比如 input 組件可以使用 input 標(biāo)簽替代。目前已支持的可替代組件列表:
還有一部分內(nèi)置組件在 html 中沒有標(biāo)簽可替代,那就需要使用 wx-component 標(biāo)簽或者使用 wx- 前綴,基本用法如下:
如果使用 wx-component 標(biāo)簽表示要渲染小程序內(nèi)置組件,然后 behavior 字段表示要渲染的組件名;其他組件屬性傳入和官方文檔一致,事件則采用 vue 的綁定方式。
wx-component 或 wx- 前綴已支持內(nèi)置組件列表:
- cover-image 組件
- cover-view 組件
- movable-area 組件
- movable-view 組件
- scroll-view 組件
- swiper 組件
- swiper-item 組件
- view 組件
- icon 組件
- progress 組件
- text 組件
- button 組件
- editor 組件
- form 組件
- picker 組件
- picker-view 組件
- picker-view-column 組件
- slider 組件
- switch 組件
- navigator 組件
- camera 組件
- image 組件
- live-player 組件
- live-pusher 組件
- map 組件
- ad 組件
- official-account 組件
- open-data 組件
- web-view 組件
內(nèi)置組件的子組件會(huì)被包裹在一層自定義組件里面,因此內(nèi)置組件和子組件之間會(huì)隔著一層容器,該容器會(huì)追加 h5-virtual 到 class 上(除了 view、cover-view、text、scroll-view 和 picker-view 組件外,因?yàn)檫@些組件需要保留子組件的結(jié)構(gòu),所以沿用 0.x 版本的渲染方式)。
0.x 版本:在 0.x 版本中,絕大部分內(nèi)置組件在渲染時(shí)會(huì)在外面多包裝一層自定義組件,可以近似認(rèn)為內(nèi)置組件和其父級(jí)節(jié)點(diǎn)中間會(huì)多一層 div 容器,所以會(huì)對(duì)部分樣式有影響。這個(gè) div 容器會(huì)追加一個(gè)名為 h5-xxx 的 class,例如使用 video 組件,那么會(huì)在這個(gè) div 容器上追加一個(gè)名為 h5-video 的 class,以便對(duì)其做特殊處理。另外如果是用 wx-component 或是 wx- 前綴渲染的內(nèi)置組件,會(huì)在容器追加的 class 是 h5-wx-component,為了更方便進(jìn)行識(shí)別,這種情況會(huì)再在容器額外追加 wx-xxx 的 class。
生成的結(jié)構(gòu)大致如下:
; PS:button 標(biāo)簽不會(huì)被渲染成 button 內(nèi)置組件,同理 form 標(biāo)簽也不會(huì)被渲染成 form 內(nèi)置組件,如若需要請(qǐng)按照上述原生組件使用說明使用。
PS:因?yàn)樽远x組件的限制,movable-area/movable-view、swiper/swiper-item、picker-view/picker-view-column 這三組組件必須作為父子存在才能使用,比如 swiper 組件和 swiper-item 必須作為父子組件才能使用,如:PS:原生組件的表現(xiàn)在小程序中表現(xiàn)會(huì)和 web 端標(biāo)簽有些不一樣,具體可參考原生組件說明文檔。
PS:原生組件下的子節(jié)點(diǎn),div、span 等標(biāo)簽會(huì)被渲染成 cover-view,img 會(huì)被渲染成 cover-image,如若需要使用 button 內(nèi)置組件請(qǐng)使用 wx-component 或 wx- 前綴。
PS:如果將插件配置 runtime.wxComponent 的值配置為 noprefix,則可以用不帶前綴的方式使用內(nèi)置組件。
PS:某些 Web 框架(如 react)會(huì)強(qiáng)行將節(jié)點(diǎn)屬性值轉(zhuǎn)成字符串類型。對(duì)于普通類型數(shù)組(如 wx-picker 組件的 value 屬性),字符串化會(huì)變成,連接,kbone 會(huì)自動(dòng)做解析,開發(fā)者無需處理;對(duì)于對(duì)象數(shù)組(如 wx-picker 組件的 range 屬性),如遇到被自動(dòng)轉(zhuǎn)成字符串的情況,開發(fā)者需要將此對(duì)象數(shù)組轉(zhuǎn)成 json 串傳入。
在 kbone-advanced
目錄下創(chuàng)建 03-native-components
目錄。本案例在這個(gè)目錄下實(shí)現(xiàn)。
cd 03-native-components
npm init -y
復(fù)制代碼
編輯 package.json:
{
"scripts": {
"mp": "cross-env NODE_ENV=production webpack --config build/webpack.mp.config.js --progress --hide-modules"
},
"dependencies": {
"vue": "^2.5.11"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.4",
"html-webpack-plugin": "^4.0.0-beta.5",
"mini-css-extract-plugin": "^0.5.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"stylehacks": "^4.0.3",
"vue-loader": "^15.7.0",
"vue-template-compiler": "^2.6.10",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3",
"mp-webpack-plugin": "latest"
}
}
復(fù)制代碼
安裝依賴包:
npm install
復(fù)制代碼
在 03-native-components
目錄下創(chuàng)建 build
文件夾,在文件夾下創(chuàng)建 webpack.mp.config.js
文件,內(nèi)容如下:
const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin')
const MpPlugin = require('mp-webpack-plugin') // 用于構(gòu)建小程序代碼的 webpack 插件
const isOptimize = false // 是否壓縮業(yè)務(wù)代碼,開發(fā)者工具可能無法完美支持業(yè)務(wù)代碼使用到的 es 特性,建議自己做代碼壓縮
module.exports = {
mode: 'production',
entry: {
index: path.resolve(__dirname, '../src/index/main.mp.js'),
},
output: {
path: path.resolve(__dirname, '../dist/mp/common'), // 放到小程序代碼目錄中的 common 目錄下
filename: '[name].js', // 必需字段,不能修改
library: 'createApp', // 必需字段,不能修改
libraryExport: 'default', // 必需字段,不能修改
libraryTarget: 'window', // 必需字段,不能修改
},
target: 'web', // 必需字段,不能修改
optimization: {
runtimeChunk: false, // 必需字段,不能修改
splitChunks: { // 代碼分隔配置,不建議修改
chunks: 'all',
minSize: 1000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 100,
maxInitialRequests: 100,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
},
minimizer: isOptimize ? [
// 壓縮CSS
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.(css|wxss)$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', {
discardComments: {
removeAll: true,
},
minifySelectors: false, // 因?yàn)?wxss 編譯器不支持 .some>:first-child 這樣格式的代碼,所以暫時(shí)禁掉這個(gè)
}],
},
canPrint: false
}),
// 壓縮 js
new TerserPlugin({
test: /\.js(\?.*)?$/i,
parallel: true,
})
] : [],
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.js$/,
use: [
'babel-loader'
],
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
extensions: ['*', '.js', '.vue', '.json']
},
plugins: [
new webpack.DefinePlugin({
'process.env.isMiniprogram': process.env.isMiniprogram, // 注入環(huán)境變量,用于業(yè)務(wù)代碼判斷
}),
new MiniCssExtractPlugin({
filename: '[name].wxss',
}),
new VueLoaderPlugin(),
new MpPlugin(require('./miniprogram.config.js')),
],
}
復(fù)制代碼
在 03-native-components
文件夾下創(chuàng)建 miniprogram.config.js
文件,內(nèi)容如下:
module.exports = {
origin: 'https://test.miniprogram.com',
entry: '/',
router: {
index: [
'/',
],
},
redirect: {
notFound: 'index',
accessDenied: 'index',
},
generate: {
appWxss: 'none',
// 構(gòu)建完成后是否自動(dòng)安裝小程序依賴。'npm':使用 npm 自動(dòng)安裝依賴
autoBuildNpm: 'npm'
},
runtime: {
// wxComponent: 'noprefix',
wxComponent: 'default'
},
app: {
navigationBarTitleText: 'miniprogram-project',
},
projectConfig: {
appid: '',
projectname: 'native-components',
},
packageConfig: {
author: 'Felixlu',
},
}
復(fù)制代碼
在 03-native-components
目錄下創(chuàng)建 index.html
文件,內(nèi)容如下:
v
在 03-native-components/src/index
目錄下創(chuàng)建 main.mp.js
小程序入口文件,內(nèi)容如下:
import Vue from 'vue'
import App from './App.vue'
export default function createApp() {
const container = document.createElement('div')
container.id = 'app'
document.body.appendChild(container)
return new Vue({
el: '#app',
render: h => h(App)
})
}
相關(guān)案例查看更多
相關(guān)閱讀
- 小程序開發(fā)公司
- 云南小程序開發(fā)制作
- 商標(biāo)
- 小程序開發(fā)聯(lián)系方式
- 小程序公司
- 高端網(wǎng)站建設(shè)公司
- 汽車報(bào)廢管理系統(tǒng)
- 汽車報(bào)廢
- 網(wǎng)站建設(shè)案例
- 微信分銷
- 手機(jī)網(wǎng)站建設(shè)
- 云南做百度小程序的公司
- 云南網(wǎng)站建設(shè)
- 做網(wǎng)站
- 網(wǎng)站開發(fā)公司哪家好
- 微信小程序開發(fā)入門課程
- 大理小程序開發(fā)
- 南通小程序制作公司
- 云南旅游網(wǎng)站建設(shè)
- 怎么做網(wǎng)站
- 網(wǎng)站優(yōu)化哪家好
- 小程序退款
- 開發(fā)框架
- 云南小程序被騙蔣軍
- web教程
- 云南網(wǎng)站建設(shè)公司排名
- 云南省建設(shè)廳官方網(wǎng)站
- 網(wǎng)站建設(shè)首頁(yè)
- 前端技術(shù)
- 買小程序被騙