How to use mobile component library in Vue.js (detailed tutorial)
This article mainly introduces cube-ui, a mobile component library based on Vue.js. Now I share it with you and give it as a reference.
cube-ui is an exquisite mobile component library implemented by Didi's technical team based on Vue.js. It's great. Although there are not many components, it is enough for basic scenarios. Thanks for the open source!
First create a vue project
vue init webpack my-project cd my-project npm install
Install cube-ui
npm install cube-ui -S
The official recommendation is to use the babel-plugin-transform-modules plug-in, which can introduce component modules and corresponding modules more elegantly style.
npm install babel-plugin-transform-modules -D
Then configure this plug-in and modify .babelrc: (Add to plugins)
{ "plugins": [ ["transform-modules", { "cube-ui": { "transform": "cube-ui/lib/${member}", "kebabCase": true, "style": { "ignore": ["create-api", "better-scroll"] } } }] ] }
Introduction method 1: Import all
General In the entry file main.js:
import Vue from 'vue' import Cube from 'cube-ui' // 一般直接放在这个位置 Vue.use(Cube)
After all are introduced, it is equivalent to global registration, and you can use it directly. There is no need to import {…} (local reference) in each .vue file, and components{…} are locally registered.
Introduction method 2: Import on demand
import { /* eslint-disable no-unused-vars */ Style, // 必需 Button } from 'cube-ui'
Note: If imported on demand, the basic style part will not be packaged, so you need to introduce style when using it module.
Registration methodOptional global registration or local registration:
// 全局注册 Vue.use(Button) // 在入口文件中 // 或者局部注册 // 某个组件中 { components: { CubeButton: Button } }
All components that can be introduced on demand:
import { Button, Checkbox, Loading, Tip, Toast, Picker, TimePicker, Dialog, ActionSheet, Scroll, Slide, IndexList } from 'cube-ui'
can also be introduced create-api and better-scroll modules:
import { createAPI, BetterScroll } from 'cube-ui'
Example
<template> <cube-button @click="showDiaog">show dialog<cube-button> </template> <script> export default { methods: { showDialog() { this.$createDialog({ type: 'alert', title: 'Alert', content: 'dialog content' }).show() } } } </script>
Do not use post-compilation
Note: cube-ui with webpack 2 will use post-compile by default, but Post-compilation requires some dependencies and configurations (see the end of this page); if you don’t want to use post-compilation, you can directly modify the webpack configuration:
// webpack.config.js module.exports = { // ... resolve: { // ... alias: { // ... 'cube-ui': 'cube-ui/lib' // ... } // ... } // ... }
Use post-compile
cube-ui combination After webpack 2, post-compilation will be used by default, so the application needs to be compatible with cube-ui's dependencies and configuration.
1. Modify package.json
{ // webpack-post-compile-plugin 依赖 compileDependencies "compileDependencies": ["cube-ui"], "devDependencies": { "babel-plugin-transform-modules": "^0.0.2", // 新增 stylus 相关依赖 (都需要额外安装:npm install … -D) // stylus 类似于 sass,less "stylus": "^0.54.5", "stylus-loader": "^2.1.1", "webpack-post-compile-plugin": "^0.1.2" } }
2. Modify .babelrc and still rely on babel-plugin-transform-modules:
"plugins": [ ["transform-runtime"], ["transform-modules", { "cube-ui": { // 注意: 这里的路径需要修改到 src/modules 下 "transform": "./node_modules/cube-ui/src/modules/${member}", "kebabCase": true } }] ]
3. Modify webpack.base.conf .js
var PostCompilePlugin = require('webpack-post-compile-plugin') module.exports = { // ... plugins: [ // ... new PostCompilePlugin() ] // ... }
4. Modify the exports.cssLoaders function in build/utils.js
exports.cssLoaders = function (options) { // ... const stylusOptions = { 'resolve url': true } // https://vue-loader.vuejs.org/en/configurations/extract-css.html return { css: generateLoaders(), postcss: generateLoaders(), less: generateLoaders('less'), sass: generateLoaders('sass', { indentedSyntax: true }), scss: generateLoaders('sass'), stylus: generateLoaders('stylus', stylusOptions), styl: generateLoaders('stylus', stylusOptions) } }
Run and see the results:
npm run dev
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to implement high-performance loading sequence in javascript
##How to implement global registration in axios
How to implement login verification jump using Vue Flask (detailed tutorial)
About usage of connect decorator in react-redux
The above is the detailed content of How to use mobile component library in Vue.js (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.
