VUX Mobile UI Library

repository·v2·Indexed 12 days ago

https://github.com/airyland/vux

A mobile UI component library for Vue.js (targeting Vue 2.3.0+) inspired by WeUI, specifically designed for WeChat pages. Version 2.11.1 features on-demand loading via vux-loader and follows WeUI design specifications to provide a smooth mobile development experience.

Tokens
81K
Snippets
290
Records
402
Agent score
94%

What's inside VUX

  1. Overview of VUX

    v2

    VUX is a mobile UI component library built on top of WeUI and Vue (2.x), specifically designed for WeChat pages. It provides a set of components that follow the WeUI design specifications while extending them with several common mobile components.

    Key characteristics:

    • Library, not a framework: You can freely use VUX alongside other component libraries or tools.
    • On-demand loading: When used with vux-loader, components are loaded on demand, ensuring that only the code for the components you actually use is included in your final bundle.
    • Styling flexibility: While VUX uses less as its CSS preprocessor (consistent with WeUI), the flexibility of .vue single-file components allows you to use other preprocessors like SASS in your project.
  2. What is vux-loader?

    v2

    vux-loader is an engineering tool designed for webpack + vue-loader projects. It pre-processes .vue and .js files before they are handled by vue-loader.

    While it includes optimized configurations and plugins specifically for the Vux component library, it is a general-purpose tool that can be used for various code processing tasks in any Vue project.

    Key Capabilities

    • Pre-processing: Intercepts and modifies code before vue-loader processes it.
    • Simplified Imports: Allows cleaner import syntax for Vux plugins. For example, instead of deep-importing from source paths, you can use:
      import { AlertPlugin, ToastPlugin } from 'vux'
    • Feature Switching: Supports conditional code output based on features (e.g., using template-feature-switch).
    • Component Optimization: Can be used to perform 'component slimming' during the build process to keep only the code that is actually used.
  3. Why use vux-loader

    v2

    The vux-loader is designed to simplify development by providing three core capabilities:

    1. On-demand component loading: Allows you to import only the components you need, reducing bundle size.
    2. Less variable definition: Enables the definition and use of Less variables within your project.
    3. Compile-time optimizations: Performs code optimizations during the build process.

    To integrate it into an existing project, you simply need to use the merge method to combine the vux-loader configuration with your existing Webpack configuration.

  4. Accessing global methods via Vue vs Prototype

    v2

    There are two ways to access global methods:

    1. Via Prototype: Mount to Vue.prototype to access the method using this.$method inside component options. This is the recommended approach for component-level convenience.
    2. Via Vue Object: You can also attach methods directly to the Vue object (e.g., Vue.method = ...). While this makes the method globally available, you will need to import Vue from 'vue' inside every component where you want to use it.
  5. Understand the role of vux-loader

    v2

    The vux-loader is a Webpack loader for the VUX component library that enables features like on-demand loading (按需加载).

    Important Notes:

    • It is not a replacement for vue-loader; instead, it works alongside vue-loader.
    • If you are using the vux2 template, you do not need to configure or use it manually.
  6. Understanding duplicate styles during debugging

    v2
    If you observe multiple duplicate styles when inspecting elements during development/debugging, this is expected behavior. Different components may reference the same Less source code, and during debugging, these styles are inserted into the page via individual <style> tags for each component.
  7. Understand VUX Virtual Components

    v2

    A virtual component is a concept unique to VUX. It refers to a component that can be used in your templates without an explicit import statement. During the build process, vux-loader detects these components and replaces them with actual html code.

    Key benefits include:

    • On-demand loading: It allows for efficient resource management. For example, instead of importing all svg icons into a component, vux-loader can read the corresponding svg icon based on a component's attribute and inline it directly at compile time (e.g., using an x-icon component).
    • Special attribute handling: It enables support for special attribute assignments or custom syntax that standard Vue components might not handle natively.
  8. Understand the UMD build directory structure

    v2

    The generated files are located in the dist/ directory. Note that even sub-components are organized into their own folders and include an empty index.min.css file for consistency.

    Directory Layout:

    • dist/vux.min.js: A bundle containing all components, plugins, and the default address library. Note: Only for testing; do not use in production.
    • dist/vux.min.css: A bundle containing all component styles. Note: Only for testing; do not use in production.
    • dist/components/[component-name]/index.min.js: The JavaScript code for a specific component.
    • dist/components/[component-name]/index.min.css: The CSS code for a specific component.

    Global Variable Mapping: When using vux.min.js, everything is mounted to the global vux variable and the window object:

    • Components: vuxGroup, vuxCell, etc.
    • Plugins: vuxAlertPlugin, etc.
    • Address Library: vuxChinaAddressData
    |- dist/
      |- vux.min.js ------------ All components bundled (testing only)
      |- vux.min.css ----------- All styles bundled (testing only)
      |- components
          |- actionsheet
            |- index.min.js -------- Component JS
            |- index.min.css ------- Component CSS
  9. Advanced: Use a function as the transformer (Babel 7+)

    v2

    If you are using Babel 7+ and a .babelrc.js file, you can provide a function directly to the transform option for complex logic. The function receives importName and matches as arguments.

    module.exports = {
        presets: ['@babel/env'],
        plugins: [
            ['transform-imports', {
                'my-library': {
                    transform: function(importName, matches) {
                        return `my-library/etc/${importName.toUpperCase()}`;
                    },
                    preventFullImport: true,
                }
            }]
        ]
    };