zoukankan      html  css  js  c++  java
  • VUE 组件 + el 安装配置

    ###1.  定义子组件
    
    ```
    ​```javascript
    <template>
      <div>{{data}}
      </div>
    </template>
    <script>
    export default {
      props: ['data'], // 接收父组件给子组件定义的属性
    }
    </script>
    ​```
    ```
    
    ​    
    
    ###2.  子组件引入, 注册, 使用
    
    ```
    ​```js
    <template>
      <div>
        父组件显示:{{msg}}
        <DemoComp
          :data='msg'
        ></DemoComp>
      </div>
    </template>
    <script>
    import DemoComp from '@/components/DemoComp'
    export default {
      components: {
        DemoComp
      },
      data() {
        return {
          msg: '父组件的信息'
        }
      },
      methods: {
      }
    }
    </script>
    
    ​```
    ```
    
    ###3.  给子组件添加事件及事件处理方法
    
    ```
    ​```javascript
    <template>
      <div>
        父组件显示:{{msg}}
        <DemoComp
          :data='msg'
          @changeMsg='change'
        ></DemoComp>
      </div>
    </template>
    <script>
    import DemoComp from '@/components/DemoComp'
    export default {
      components: {
        DemoComp
      },
      data() {
        return {
          msg: '父组件的信息'
        }
      },
      methods: {
        change(data) {
          // debugger
          // alert('调用了父组件的方法, 接收到信息:'+data)
          this.msg = data  // 更新父组件的内容
        }
      }
    }
    </script>
    
    ​```
    ```
    
    ###4.  子组件通过触发方法, 向父组件传值
    
    ```
    ​```javascript
    <template>
      <div>{{data}}
    
        <button @click="emitfather">调用父组件方法</button>
      </div>
    </template>
    <script>
    export default {
      props: ['data'], // 接收父组件给子组件定义的属性
      methods: {
        emitfather() {
          this.$emit('changeMsg', '子组件信息')
        }
      }
    }
    </script>
    ​```
    ```
    
    ###5.  属性.sync 方式, 来更新父组件的信息, 父组件内容
    
    ```
    ​```javascript
    <DemoComp
          :data.sync='msg'
    ></DemoComp>
    ​```
    ```
    
    ###6.  子组件内容:
    
    ```
    ​```javascript
    <template>
      <div>{{data}}
    
        <button @click="emitfather">调用父组件方法</button>
        <button @click="$emit('update:data', '使用sync更新父组件')">更新父组件信息</button>
      </div>
    </template>
    <script>
    export default {
      props: ['data'], // 接收父组件给子组件定义的属性
      methods: {
        emitfather() {
          this.$emit('changeMsg', '子组件信息')
        }
      }
    }
    </script>
    ​```
    ```
    
    ###7.  element-ui的安装和引入
    
    ```
    ​```javascript
    // 1. 安装:   npm install element-ui
    // 2. 配置:   
    // main.js文件
    
    import Vue from 'vue'
    import App from './App'
    import router from './router'
    
    //========== 增加这部分内容
    // 引入 elementui
    import ElementUI from 'element-ui'
    import 'element-ui/lib/theme-chalk/index.css'
    Vue.use(ElementUI)
    //===========
    ```
    
    ​    Vue.config.productionTip = false
    ​    /* eslint-disable no-new */
    ​    new Vue({
    ​      el: '#app',
    ​      router,
    ​      components: { App },
    ​      template: '<App/>'
    ​    })
    ​    
    
    ```
    ​```
    ```
    
    ###8.  element-ui的使用
    
    ```
    ​```javascript
    // 组件中直接使用 element-ui的组件即可
    <template>
      <div>
        <el-dialog
          title="新增图书"
          :visible="visible"
        >
              ...
        </el-dialog>
      </div>
    </template>
    ​```
    
    ```
    
    ​    
  • 相关阅读:
    冯小刚贺岁片十大经典台词
    网络”X客”大集合:博客、维客、奇客、播客、闪客、摩客、威克…
    [ASP.NET]动态页面调用JS错误。保存为HTML文件就不报错了。
    xp sp2 pro 安装IIS时候出现 安装程序无法复制文件staxmem.dl_
    上传功能出现 asp 0104 不允许操作解决方法
    插入数据库记录时候“输入字符串的格式不正确。 ”
    猪的FLASH-星晴
    Tab Bar Controller 与 Navigation Controller 共存
    iPhone控件之UILabel
    使用MPMoviePlayerViewController播放视频
  • 原文地址:https://www.cnblogs.com/mldsh/p/13492662.html
Copyright © 2011-2022 走看看