Code Monkey home page Code Monkey logo

vue's Introduction

my-project

A Vue.js project

环境安装

# 全局安装 cnpm 
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
# 全局安装 vue-cli
$ npm install --global vue-cli
# 创建一个基于 webpack 模板的新项目
$ vue init webpack my-project
# 安装依赖,走你
$ cd my-project
$ npm install

运行与构建

# 运行
npm run dev

# 打包
npm run build

# 打包并查看具体打包信息
npm run build --report

# 运行 e2e 测试
npm run e2e

# 运行所有测试
npm test

组件划分

vuejs组件之间的调用-另外一个重要选项-components

import Header from './header'
import Footer from '/footer'
new Vue({
  data: {
      isShow: true
  },
  components: [
      Header,Footer
  ]
})

调用的时候只需要直接使用标签

<header></header>
<footer></footer>

###vuejs组件之间的通信 - props

new Vue({
  data: {
      username: 'xxx'
  },
  props" ['msg],
  methods: {
    dothis: function () {
        console.log(this.msg)
  	}
  }
})
<!-- this is app.vue -->
<header msg='something interesting'></header>
<footer></footer>

父组件向子组件传递信息

先创建一个ComponentA组件

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button v-on:click="clickMe">Click!</button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      msg: 'Welcome to Component A'
    }
  },
  methods: {
    clickMe: function () {
      
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
</style>

将ComponentA注册到App.vue中

<template>
  <div id="app">
    <ComponentA></ComponentA>  //在template中添加组件
  </div>
</template>

<script>
import ComponentA from './components/ComponetA'    // 引入组件
export default {
  name: 'app',
  data () {
    return {
      
    }
  },
  components: { ComponentA },   // 注册组件
  methods: {
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

在App.vue的CompenentA中加入属性值

<ComponentA msgfromfather="father"></ComponentA>

在组件中获取属性值

<script>
export default {
  data () {
    return {
      msg: 'Welcome to Component A'
    }
  },
  props: [ 'msgfromfather' ],    //声明msgfromfather属性
  methods: {
    clickMe: function () {
      console.log(this.msgfromfather)  //获取msgfromfather属性值
    }
  }
}
</script>

子组件向父组件传递信息

<template>
  <div id="app">
    <p> child tell me: {{ childWords }} </p>
    <ComponentA msgfromfather="father"
        v-on:child-tell-me-something="listenToMyBoy"></ComponentA>
  </div>
</template>

<script>
import ComponentA from './components/ComponetA'
export default {
  name: 'app',
  data () {
    return {
      childWords: ''
    }
  },
  components: { ComponentA },
  methods: {
    listenToMyBoy: function (msg) {
      console.log('vnwenwp' + msg)
      this.childWords = msg
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
<template>
  <div class="hello">
    <h1>{{ msgfromfather }}</h1>
    <button v-on:click="clickMe">open mouse</button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      msg: 'Welcome to Component A'
    }
  },
  props: [ 'msgfromfather' ],
  methods: {
    clickMe: function () {
      this.$emit('child-tell-me-something', this.msg)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
</style>

vue's People

Contributors

xvjialing avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.