开始 vue-cli webpack 打包工具的demo

16 篇文章 0 订阅
8 篇文章 0 订阅

在命令行执行如下语句

npm install -g vue-cli
vue init webpack-simple my-project
cd my-project
npm install
npm run dev

以上步骤参考 点击打开链接

完成以上步骤例子就可以跑起来了,npm install 可能会出问题,失败的同学可以去试试 cnpm

如果 npm install 失败

npm install -g cnpm --registry=https://registry.npm.taobao.org
然后

cnpm install
npm run dev

首页做了如下调整  Hello.vue

  1. 绑定事件
  2. 数据绑定
  3. 模型绑定
  4. if
  5. for
  6. 子组件 todo-item
  7. 子组件事件
  8. 样式(scoped 作用域)

<template>
  <div class="hello">
    <h1 @click="changeTitle">{{ msg }}</h1>
    Input text to change title. <input v-model="msg">
    <h2 :title="msg">Essential Links</h2>
    <h2 v-if="show">I would toggel if you click the title. </h2>
    
    <ol>
      <todo-item v-for="list in lists" 
      :value="list.id" 
      @click.native="showIdentify">{{list.text}}</todo-item>
    </ol>
  </div>
</template>
<style scoped>
  input {
    width: 300px;
    height: 36px;
    font-size: 18px;
  }
  li {
    cursor: pointer;
  }
</style>

<script>

export default {
  name: 'hello',
  data () {
    return {
      msg: 'Awesome Vue.js App',
      show: true,
      lists: [
        { text: 'Learn JavaScript', id: '1' },
        { text: 'Learn Vue', id: '2' },
        { text: 'Build something awesome', id: '3' }
      ]
    }
  },
  methods: {
    changeTitle() {
      this.msg = 'Wonderful Vue.js App'
      this.show = !this.show
      this.lists.push({text: 'New item'})
    },
    showIdentify(evt) {
      console.info(evt.target.getAttribute('value'))
    }
  }
}
</script>

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

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>

引导也做了如下调整 main.js

注册了一个子组件

import Vue from 'vue'
import App from './App'

Vue.component('todo-item', {
  render(createElement) {
    return createElement(
      'li',
      this.$slots.default
    )
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App)
})

最后再运行下吧


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值