### Basic Form Usage Example
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/form.md
Demonstrates the basic setup of the Form component, including binding data models, defining validation rules, and integrating with Form-item and Input components. It also shows how to handle user interactions like selecting from an action sheet.
```vue
```
--------------------------------
### Project Setup and Integration
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/read.md
Guides through the essential steps to integrate uView UI into a Vue.js project. This includes importing the uView library in main.js, adding global styles in App.vue, importing SCSS variables in uni.scss, and configuring easycom for automatic component imports in pages.json.
```javascript
// main.js
import uView from 'uview-ui';
Vue.use(uView);
```
```css
/* App.vue */
```
```css
/* uni.scss */
@import "uview-ui/themb.scss";
```
```javascript
// pages.json
{
"easycom": {
"^u-(.*)": "uview-ui/components/u-$1/u-$1.vue"
},
// 此为本身已有的内容
"pages": [
// ......
]
}
```
--------------------------------
### u-input Basic Usage Example
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/input.md
Demonstrates the fundamental setup of the u-input component, including placeholder, border configuration, v-model binding, and handling the change event. This is the standard way to integrate u-input within a Vue template.
```html
```
```javascript
export default {
data() {
return {
value: ''
}
},
methods: {
change(e) {
console.log('change', e);
}
}
}
```
--------------------------------
### Making HTTP Requests with uView
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/js/http.md
Provides examples of how to send POST and GET requests using the defined API functions and directly via `uni.$u.http`. It illustrates using promises with `.then()` and `.catch()`, and `async/await` syntax.
```javascript
import { postMenu, getMenu } from '/config/api.js';
// 发出post,假设需要带上token
postMenu({ custom: { auth: true }}).then(() => {
}).catch(() =>{
})
// await等待,注意与async结合使用
// await postMenu({ custom: { auth: true }})
// 假设不需要在响应拦截器中自动弹出的toast,以及不想写catch(如果promise中进行reject,但是却没有catch的话会报错)
postMenu({ custom: { auth: true, toast: false, catch: false }}).then(() => {
})
// get请求
getMenu({ custom: { auth: true }}).then(() => {
}).catch(() =>{
})
// 也可以直接通过uni.$u.post发出请求,注意此处需要写上接口地址
// uni.$u.http.post('/common/menu', { custom: { auth: true }}).then(() => {
// }).catch(() =>{
// })
```
--------------------------------
### Install uView UI
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/read.md
Installs the uView UI framework into your project using npm. This is the first step to integrating uView into your uni-app project.
```bash
npm i uview-ui
```
--------------------------------
### Install Sass and Sass-loader
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/downloadSetting.md
Ensures Sass/SCSS compilation support, which is a dependency for uView. This command installs the necessary packages as development dependencies. It's recommended for projects not using HBuilder X's built-in SCSS support.
```js
// Install sass
npm i sass -D
// Install sass-loader
npm i sass-loader -D
```
--------------------------------
### Import uView Global SCSS Theme
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/downloadSetting.md
Applies the global SCSS theme provided by uView. This file should be imported into your project's uni.scss file to ensure consistent styling across your application. It sets up the foundational theme variables and styles.
```scss
/* uni.scss */
@import '@/uni_modules/uview-ui/theme.scss';
```
--------------------------------
### Install Project Dependencies
Source: https://github.com/umicro/uview2.0-doc/blob/master/README.md
Installs all necessary project dependencies for uView 2.0 using npm. This command reads the package.json file and downloads the required libraries.
```bash
npm install
```
--------------------------------
### Serve uView 2.0 Documentation Locally
Source: https://github.com/umicro/uview2.0-doc/blob/master/README.md
Starts a local development server to preview the uView 2.0 documentation. This is useful for development and testing changes to the documentation.
```bash
npm run serve
```
--------------------------------
### uView Framework Install Method
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/vueUse.md
Demonstrates the `install` method defined within the uView framework. This method is designed to be compatible with Vue.use, accepting the Vue instance as its first parameter and attaching the `$u` object to `Vue.prototype` for global access.
```javascript
// 这里我们定义了一个叫"install"的变量,它的内容是一个方法(函数)
// 它的第一个参数是Vue对象(上面有提到传进来的第一个参数就是Vue),我们把$u挂载到了Vue.prototype中
const install = (Vue) => {
Vue.prototype.$u = $u;
}
// 这里我们导出一个对象,内部有一个叫"install"的方法,给上面说的Vue.use调用
export default {
install
}
```
--------------------------------
### Configure easycom Component Mode
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/downloadSetting.md
Sets up the easycom component mode in your project's pages.json file, allowing for automatic component registration. This configuration enables uView components to be used directly without explicit imports. Note that changes to easycom rules require a project restart or recompile to take effect.
```json
{
// 如果您是通过uni_modules形式引入uView,可以忽略此配置
"easycom": {
"^u-(.*)": "@/uni_modules/uview-ui/components/u-$1/u-$1.vue"
},
// 此为本身已有的内容
"pages": [
// ......
]
}
```
--------------------------------
### Install uView UI Package
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/npmSetting.md
Installs the uView UI framework version 2.0.38. If your HBuilder X project lacks a package.json file, run 'npm init -y' first.
```js
// 如果您的项目是HX创建的,根目录又没有package.json文件的话,请先执行如下命令:
// npm init -y
// 安装
npm install uview-ui@2.0.38
```
--------------------------------
### Import uView Main JS Library
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/downloadSetting.md
Integrates the uView UI framework into your Vue application by importing and using the main JS library. This step is essential for uView's core functionality and must be placed after the Vue import in your main.js file.
```js
// main.js
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
```
--------------------------------
### Import uView Base Styles
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/downloadSetting.md
Includes uView's base styles, which are crucial for the framework's components to render correctly. This import statement must be placed at the very first line within the style block of your App.vue file, and the style tag must have the lang="scss" attribute.
```css
```
--------------------------------
### Basic CountTo Usage
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/countTo.md
Demonstrates the basic usage of the CountTo component by setting the start and end values.
```html
```
--------------------------------
### Vue.js uni-app Example
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/js/guid.md
Demonstrates using the guid function to assign a unique ID to a view element and a class name to a data property within a uni-app component.
```html
```
```javascript
export default{
data() {
return {
elClass: uni.$u.guid(20),
}
}
}
```
--------------------------------
### Get Current Page Path (page)
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/js/fastUse.md
Returns the path of the current page, starting with a '/'. This is a simple utility to retrieve the current route.
```js
// Returns a path like '/pages/example/components'
uni.$u.page()
```
--------------------------------
### Vue Form Validation Example
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/form.md
Demonstrates how to use the u-form component for form validation in a Vue.js application. It includes template setup, data binding, validation rules, and the submit method to trigger validation.
```html
提交
```
```javascript
export default {
data() {
return {
form: {
name: '',
},
rules: {
name: [
{
required: true,
message: '请输入姓名',
trigger: ['blur', 'change']
}
]
}
};
},
methods: {
submit() {
this.$refs.uForm.validate().then(res => {
uni.$u.toast('校验通过')
}).catch(errors => {
uni.$u.toast('校验失败')
})
}
},
};
```
--------------------------------
### Basic Usage of u-gap Component
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/gap.md
Demonstrates the basic implementation of the u-gap component by directly importing it. It shows how to configure the height and background color using props.
```html
```
--------------------------------
### Build uView 2.0 Project
Source: https://github.com/umicro/uview2.0-doc/blob/master/README.md
Compiles and builds the uView 2.0 project for production. This command typically bundles assets and optimizes code for deployment.
```bash
npm run build
```
--------------------------------
### Basic Steps Usage
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/steps.md
Demonstrates the basic usage of the Steps component to display a sequence of steps with titles and descriptions. The 'current' prop indicates the active step.
```html
```
--------------------------------
### guid Function API
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/js/guid.md
Generates a globally unique, random GUID. Supports custom length, a mandatory 'u' prefix for valid IDs/classes, and configurable radix for character sets.
```APIDOC
guid(length = 32, firstU = true, radix = 62)
Generates a globally unique, random GUID.
- length : The length of the GUID, defaults to 32. If null, generates according to RFC4122 standard.
- firstU : Whether the first letter is 'u'. Defaults to true. Useful as IDs/classes cannot start with a number.
- radix : The base for generating the random string, defaults to 62. Uses characters "0-9A-Za-z".
Note: All parameters have default values, making it callable without arguments. Recommended to call without arguments.
```
--------------------------------
### Basic Usage Example
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/code.md
Demonstrates the basic integration of the u-code component, including setting seconds, handling change events, and triggering the countdown via a button tap. It shows how to use refs to interact with the component's methods and properties like `canGetCode`.
```vue
{{tips}}
```
--------------------------------
### Basic Usage of u-empty Component
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/empty.md
Demonstrates the basic implementation of the u-empty component, allowing customization of the mode (icon) and providing a link to download default assets.
```html
```
--------------------------------
### Vue.use Method Implementation
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/vueUse.md
Illustrates the core logic of Vue's `use` method. It details how the method accepts a plugin, extracts arguments, and conditionally calls the plugin's `install` method or the plugin itself, passing the Vue instance as the first argument.
```javascript
Vue.use = function (plugin: Function | Object) {
// ......
const args = toArray(arguments, 1)
// 这一句很重要,这里的this,就是Vue,把他添加到args数组的第一个元素
args.unshift(this)
// 判断我们传递进来的"uView",也即这里的"plugin"内部是否有一个叫"install"的方法
// 如果有,就执行我们的"uView",也即"plugin.install"方法
if (typeof plugin.install === 'function') {
plugin.install.apply(plugin, args)
} else if (typeof plugin === 'function') {
plugin.apply(null, args)
}
// ......
}
```
--------------------------------
### Install Sass and Sass-loader
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/npmSetting.md
Installs the necessary Sass and sass-loader packages for uView UI. It's recommended to use sass-loader version 10 to avoid compatibility issues with Vue and Sass.
```js
// 安装sass
npm i sass -D
// 安装sass-loader,注意需要版本10,否则可能会导致vue与sass的兼容问题而报错
npm i sass-loader@10 -D
```
--------------------------------
### NoticeBar API Documentation
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/noticeBar.md
Comprehensive API documentation for the NoticeBar component, detailing all available props and events for customization and interaction.
```APIDOC
NoticeBar Component API:
Props:
- text: (Array | String) - The content to display. Must be an array when `direction` is 'column', and a string when `direction` is 'row'.
- direction: (String) - The scrolling mode. 'row' for horizontal scrolling, 'column' for vertical scrolling. Defaults to 'row'.
- step: (Boolean) - Whether to use step-by-step scrolling when `direction` is 'row'. Defaults to false.
- icon: (String) - The icon to display on the left. Defaults to 'volume'.
- mode: (String) - The announcement mode. 'link' displays a right arrow, 'closable' displays a close button on the right. Optional values: 'link', 'closable'.
- color: (String) - The text color. Defaults to '#f9ae3d'.
- bgColor: (String) - The background color. Defaults to '#fdf6ec'.
- speed: (String | Number) - The scrolling speed in px/rpx per second for horizontal scrolling. This helps maintain a consistent speed regardless of text length. Defaults to 80.
- fontSize: (String | Number) - The font size of the text. Defaults to 14.
- duration: (String | Number) - The duration in milliseconds for one full scroll cycle. Defaults to 2000.
- disableTouch: (Boolean) - Whether to disable touch gestures for switching. Supported on App 2.5.5+, H5 2.5.5+, Alipay Mini Program, ByteDance Mini Program. Defaults to true.
- url: (String) - The path to navigate to when the announcement is clicked.
- linkType: (String) - The type of page navigation. Defaults to 'navigateTo'.
Events:
- click: Triggered when the announcement text is clicked.
- Callback Parameters: `index` (Number) - The index of the clicked text item (relevant for array content).
- close: Triggered when the close icon on the right is clicked.
- Callback Parameters: None.
```
--------------------------------
### Basic u-image Usage
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/image.md
Demonstrates the fundamental usage of the u-image component by configuring its width, height, and source URL. It also shows how to enable the loading indicator.
```html
```
--------------------------------
### Manual Control (Start, Pause, Reset)
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/countDown.md
Illustrates how to manually control the countdown timer using its `ref`. The component exposes methods like `start()`, `pause()`, and `reset()` which can be called from the parent component's script to manage the timer's state.
```html
```
--------------------------------
### JavaScript Validator Example with uView Test
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/form.md
Demonstrates using a custom validator in uView to integrate with built-in testing functions like `uni.$u.test.mobile`. This example shows how to validate a mobile number field, returning true for valid and false for invalid input.
```javascript
rules: {
// 字段名称
mobile: [
{
required: true,
message: '请输入手机号',
trigger: ['change','blur'],
},
{
// 自定义验证函数,见上说明
validator: (rule, value, callback) => {
// 上面有说,返回true表示校验通过,返回false表示不通过
// uni.$u.test.mobile()就是返回true或者false的
return uni.$u.test.mobile(value);
},
message: '手机号码不正确',
// 触发器可以同时用blur和change
trigger: ['change','blur'],
}
]
}
```
--------------------------------
### 支付宝小程序: 开启 component2 模式
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/feature.md
为支持支付宝小程序中的 `provide/inject`、`$slots` 等特性,需要在 `manifest.json` 中开启 `component2` 模式。此配置项对于支付宝小程序至关重要。
```json
{
"mp-alipay" : {
"component2": true
}
}
```
--------------------------------
### u-loading-page Component API Documentation
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/loadingPage.md
Comprehensive API documentation for the u-loading-page component, detailing all available props, their types, default values, and descriptions.
```APIDOC
u-loading-page Component Props:
- loadingText:
- Description: 提示内容 (Prompt content)
- Type: String | Number
- Default: 正在加载 (Loading...)
- Optional Values: -
- image:
- Description: 文字上方用于替换loading动画的图片 (Image used above text to replace loading animation)
- Type: String
- Default: -
- Optional Values: -
- loadingMode:
- Description: 加载动画的模式 (Loading animation mode)
- Type: String
- Default: circle
- Optional Values: spinner, semicircle
- loading:
- Description: 是否加载中 (Whether it is loading)
- Type: boolean
- Default: false
- Optional Values: true
- bgColor:
- Description: 背景颜色 (Background color)
- Type: String
- Default: #ffffff
- Optional Values: -
- color:
- Description: 文字颜色 (Text color)
- Type: String
- Default: #C8C8C8
- Optional Values: -
- fontSize:
- Description: 文字大小 (Text size)
- Type: String | Number
- Default: 19
- Optional Values: -
- iconSize:
- Description: 图标大小 (Icon size)
- Type: String | Number
- Default: 28
- Optional Values: -
- Note: Introduced in v2.0.32
- loadingColor:
- Description: 加载中图标的颜色 (Color of the loading icon)
- Type: String
- Default: #C8C8C8
- Optional Values: -
```
--------------------------------
### Get Platform Name (os)
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/js/fastUse.md
Returns the name of the current platform in lowercase, such as 'ios' or 'android'. This is a utility function provided by uni.$u.
```js
uni.$u.os()
```
--------------------------------
### City Selection Demo and Template
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/layout/citySelect.md
This snippet demonstrates the usage of the City Selection component. It includes a demo model pointing to a specific city selection page and a template download feature. This serves as a reference for implementing city selection functionality.
```html
```
--------------------------------
### NumberBox Step Setting
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/numberBox.md
Configures the step increment/decrement value using the 'step' prop. The default step is 1; this example sets it to 2.
```vue
```
--------------------------------
### Main.js Integration of Vuex and Mixin
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/guide/globalVariable.md
Demonstrates how to import the Vuex store and the custom mixin into the main application file (`main.js`) for uni-app.
```javascript
// main.js
let vuexStore = require("@/store/$u.mixin.js");
Vue.mixin(vuexStore);
import store from '@/store';
// 将store放入Vue对象创建中
const app = new Vue({
store,
...App
})
```
--------------------------------
### Get System Info (sys)
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/js/fastUse.md
Retrieves device information, equivalent to calling uni.getSystemInfoSync(). This function provides a convenient way to access system details.
```js
uni.$u.sys()
```
--------------------------------
### CountDown Component Methods
Source: https://github.com/umicro/uview2.0-doc/blob/master/docs/components/countDown.md
Provides methods to control the countdown component programmatically, requiring access via a ref. These include starting, pausing, and resetting the timer.
```APIDOC
CountDown Component Methods:
Requires obtaining the countdown component instance via ref.
start:
Description: Starts the countdown timer.
pause:
Description: Pauses the countdown timer.
reset:
Description: Resets the countdown timer to its initial state.
```