Install 安装

  1. npm install zarm-vue --save

还在使用1.0.x版本的,请使用如下安装指令

  1. npm install zarm-vue@1.0 --save

使用Starter Kit

我们提供了通用的项目模板,你可以直接使用。如果不希望使用我们提供的模板,请继续阅读。

Import 引入

  • 全组件引入
  1. import Vue from 'vue';
  2. import zarmVue from 'zarm-vue';
  3. // 引入全局样式
  4. import 'zarm-vue/zarm-vue.default.css';
  5. Vue.use(zarmVue);
  • 按需引入

借助ElementUI提供的babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的。

首先,安装 babel-plugin-component:

  1. npm install babel-plugin-component -D

然后,将 .babelrc 添加:

  1. {
  2. // ...
  3. "plugins": [["component", {
  4. "libraryName": "zarm-vue",
  5. "styleLibraryName": "theme"
  6. }
  7. ]]
  8. }

接下来,如果你只希望引入部分组件,比如 Button 和 Alert,那么需要在 main.js 中写入以下内容:

  1. import { Button, Alert } from 'zarm-vue'
  2. Vue.use(Button)
  3. Vue.use(Alert)

全组件按需引入示例:

  1. import {
  2. Accordion,
  3. AccordionItem,
  4. Actionsheet,
  5. Alert,
  6. Badge,
  7. Button,
  8. Calendar,
  9. Cell,
  10. Checkbox,
  11. CheckboxGroup,
  12. Confirm,
  13. DatePicker,
  14. DatePickerView,
  15. DateSelect,
  16. Drag,
  17. Icon,
  18. Input,
  19. InputNumber,
  20. Keyboard,
  21. KeyboardPicker,
  22. Loading,
  23. Mask,
  24. Message,
  25. Modal,
  26. NoticeBar,
  27. Panel,
  28. PanelHeader,
  29. PanelBody,
  30. PanelFooter,
  31. Picker,
  32. PickerView,
  33. Popup,
  34. Progress,
  35. Pull,
  36. Radio,
  37. RadioGroup,
  38. SearchBar,
  39. Select,
  40. Slider,
  41. Spinner,
  42. StackPicker,
  43. Stepper,
  44. Swipe,
  45. SwipeAction,
  46. SwipeItem,
  47. Switch,
  48. Tabs,
  49. TabPane,
  50. Toast,
  51. Tooltip,
  52. Uploader,
  53. } from 'zarm-vue'
  54. const components = [
  55. Accordion,
  56. AccordionItem,
  57. Actionsheet,
  58. Alert,
  59. Badge,
  60. Button,
  61. Calendar,
  62. Cell,
  63. Checkbox,
  64. CheckboxGroup,
  65. Confirm,
  66. DatePicker,
  67. DatePickerView,
  68. DateSelect,
  69. Drag,
  70. Icon,
  71. Input,
  72. InputNumber,
  73. Keyboard,
  74. KeyboardPicker,
  75. Loading,
  76. Mask,
  77. Message,
  78. Modal,
  79. NoticeBar,
  80. Panel,
  81. PanelHeader,
  82. PanelBody,
  83. PanelFooter,
  84. Picker,
  85. PickerView,
  86. Popup,
  87. Progress,
  88. Pull,
  89. Radio,
  90. RadioGroup,
  91. SearchBar,
  92. Select,
  93. Slider,
  94. Spinner,
  95. StackPicker,
  96. Stepper,
  97. Swipe,
  98. SwipeAction,
  99. SwipeItem,
  100. Switch,
  101. Tabs,
  102. TabPane,
  103. Toast,
  104. Tooltip,
  105. Uploader,
  106. ];
  107. const install = function (Vue, opts = {}) {
  108. /* istanbul ignore if */
  109. if (install.installed) return;
  110. components.map(component => Vue.component(component.name, component));
  111. Vue.use(Loading.directive);
  112. Vue.prototype.$zaToast = Toast.root;
  113. Vue.prototype.$zaLoading = Loading.root;
  114. Vue.prototype.$zaAlert = Alert.root;
  115. Vue.prototype.$zaConfirm = Confirm.root;
  116. };
  117. install(Vue);
  • 也可以通过cdn引入umd模块
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- import CSS -->
  6. <link rel="stylesheet" href="https://unpkg.com/zarm-vue@1.1.2/zarm-vue.default.css">
  7. <script src="https://unpkg.com/vue@2.5.11/dist/vue.min.js"></script>
  8. <script src="https://unpkg.com/zarm-vue@1.1.2/zarm-vue.umd.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <za-button theme="primary">普通按钮</za-button>
  13. </div>
  14. </body>
  15. <script>
  16. new Vue({
  17. el: '#app'
  18. })
  19. </script>
  20. </html>

集成Typescript开发

  • 安装typescript相关依赖
 npm install typescript ts-loader --save-dev
  • webpack配置增加ts-loader支持示例
module.exports = {
  // ...
  resolve: {
    extensions: ['.ts','.js', '.vue', '.json'],
  },
  module: {
    rules: [
      // ...
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        include: [resolve('src'), resolve('test')],
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      }
    ]
  },

}

  • 配置tsconfig.json
{
  "compilerOptions": {
    // 与 Vue 的浏览器支持保持一致
    "target": "es5",
    // 这可以对 `this` 上的数据属性进行更严格的推断
    "strict": true,
    // 如果使用 webpack 2+ 或 rollup,可以利用 tree-shake:
    "module": "es2015",
    "moduleResolution": "node"
  },
  "include": ["./src/**/*"]
}

  • 添加vue-shims.d.ts文件
declare module "*.vue" {
 import Vue from "vue";
 export default Vue;
}

declare module 'zarm-vue' {
 const ZarmVue: any;
 export default ZarmVue;
}