4 迁移页面/组件

假如 vue 项目中 src/components/HelloWorld.vue组件内有个子组件 comp;

首先我们修改下这两个组件,使其有一些简单的新增todolist的功能

HelloWorld.vue

  1. <template>
  2. <div class="demo-com">
  3. <div class="title">this is helloworld</div>
  4. <comp @parentClick="handleParentClick"></comp>
  5. </div>
  6. </template>
  7. <script>
  8. import lodash from 'lodash'
  9. import comp from './comp.vue';
  10. export default {
  11. name: 'HelloWorld',
  12. data () {
  13. return {
  14. }
  15. },
  16. methods:{
  17. handleParentClick(...args){
  18. console.log('parentClick',...args)
  19. }
  20. },
  21. components:{
  22. comp
  23. }
  24. }
  25. </script>
  26. <!-- Add "scoped" attribute to limit CSS to this component only -->
  27. <style scoped>
  28. .demo-com {
  29. display: flex;
  30. flex-direction: column;
  31. align-items: center;
  32. height:400px;
  33. justify-content: center;
  34. }
  35. .title {
  36. align-self: center;
  37. color: #61c7fc;
  38. font-size: 72px;
  39. margin-bottom: 20px;
  40. }
  41. </style>

comp.vue

  1. <template>
  2. <div>
  3. <input type="text" v-model="todo">
  4. <div v-for="(item,index) in todos">
  5. {{item}}
  6. </div>
  7. <div @click="addTodo">addTodo</div>
  8. <div @click="handleClick">触发父组件事件</div>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'HelloWorld',
  14. data () {
  15. return {
  16. todo:'todo1',
  17. todos:[],
  18. }
  19. },
  20. methods:{
  21. addTodo(){
  22. this.todos.push(this.todo)
  23. },
  24. handleClick(){
  25. console.log('click');
  26. this.$emit('parentClick',{
  27. value:1,
  28. })
  29. }
  30. }
  31. }
  32. </script>
  33. <!-- Add "scoped" attribute to limit CSS to this component only -->
  34. <style scoped>
  35. </style>

4.1 新建页面/组件

  1. cml init page
  2. 输入 HelloWorld

利用脚手架命令,在src/pages中生成对应的页面

  1. <template>
  2. <view><text>Hello Chameleon!</text></view>
  3. </template>
  4. <script>
  5. class HelloWorld {
  6. //...
  7. }
  8. export default new HelloWorld();
  9. </script>
  10. <style>
  11. </style>
  12. <script cml-type="json">
  13. {
  14. "base": {
  15. "usingComponents": {}
  16. },
  17. "wx": {
  18. "navigationBarTitleText": "index",
  19. "backgroundTextStyle": "dark",
  20. "backgroundColor": "#E2E2E2"
  21. },
  22. "alipay": {
  23. "defaultTitle": "index",
  24. "pullRefresh": false,
  25. "allowsBounceVertical": "YES",
  26. "titleBarColor": "#ffffff"
  27. },
  28. "baidu": {
  29. "navigationBarBackgroundColor": "#ffffff",
  30. "navigationBarTextStyle": "white",
  31. "navigationBarTitleText": "index",
  32. "backgroundColor": "#ffffff",
  33. "backgroundTextStyle": "dark",
  34. "enablePullDownRefresh": false,
  35. "onReachBottomDistance": 50
  36. }
  37. }
  38. </script>
  1. cml init component
  2. 选择 Normal component
  3. 输入 comp

利用脚手架命令,在src/components中生成对应的组件

  1. <template>
  2. <view><text>Hello Chameleon!</text></view>
  3. </template>
  4. <script>
  5. class Comp {
  6. //...
  7. }
  8. export default new Comp();
  9. </script>
  10. <style>
  11. </style>
  12. <script cml-type="json">
  13. {
  14. "base": {
  15. "usingComponents": {}
  16. }
  17. }
  18. </script>

4.2 迁移组件引用

假设vue项目src/components/HelloWorld.vue中引用了其他组件 import comp from './comp.vue';

对应到cml项目 组件需要在 usingComponents 引用,不需要在配置 components字段

修改src/pages/HelloWorld/HelloWorld.cml 页面配置,如下:

  1. <script cml-type="json">
  2. {
  3. "base": {
  4. "usingComponents": {
  5. "comp":"/components/comp/comp"
  6. }
  7. }
  8. }
  9. </script>

总结

1 router.js中对应的组件需要通过 cml init page生成,然后在 router.config.js中配置对应路由

2 组件内部引用的子组件要通过cml init component 生成 ,然后通过usingComponents字段去引用

3 组件内引用的其他js库,比如import lodash from 'lodash'仍然通过import的形式引用