天天看點

Vue中元件化編碼使用(實戰練習一)

Vue中元件化編碼的大緻流程(初接觸)、元件之間的參數傳遞(最基礎的形式)、元件之間的配合完成一個需求

1、在Vue中進行元件化編碼

1.1、元件化編碼流程:

  • (1)、拆分靜态元件:元件要按照功能點拆分,命名不要與html元素沖突。
  • (2)、實作動态元件:考慮好資料的存放位置,資料是一個元件在用,還是一些元件在用:

    ​ 1)、一個元件在用:放在元件自身即可。

    2)、 一些元件在用:放在他們共同的父元件上(狀态提升)。

  • (3)、實作互動:從綁定事件開始。

1.2、props适用于:

  • (1)、父元件 ==> 子元件 通信
  • (2)、子元件 ==> 父元件 通信(要求父先給子一個函數)

1.3 、注意事項

使用v-model時要切記:v-model綁定的值不能是props傳過來的值,因為props是不可以修改的

props傳過來的若是對象類型的值,修改對象中的屬性時Vue不會報錯,但不推薦這樣做。

2、任務需求(需要實作的效果)

Vue中元件化編碼使用(實戰練習一)
Vue中元件化編碼使用(實戰練習一)

3、項目結構

Vue中元件化編碼使用(實戰練習一)

4、代碼執行個體

4.1 TheFooter.vue

<template>
  <div class="todo-footer" >

      <!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
      <input type="checkbox" />

    <span> 已完成2/全部3</span>
    <button class="btn btn-danger">清除已完成任務</button>
  </div>
</template>

<script>export default {
  name: "TheFooter",

  methods: {},
};</script>

<style scoped>/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}</style>      

4.2 TheHeader.vue

<template>
  <div class="todo-header">
    <input type="text" placeholder="請輸入你的任務名稱,按Enter鍵确認" />
  </div>
</template>

<script>export default {
  name: "TheHeader",

  data() {
    return {};
  },
  methods: {},
};</script>

<style scoped>/*header*/
.todo-header input {
  width: 560px;
  height: 28px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 7px;
}

.todo-header input:focus {
  outline: none;
  border-color: rgba(82, 168, 236, 0.8);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
    0 0 8px rgba(82, 168, 236, 0.6);
}</style>      

4.3 TheItem.vue

<template>
  <div>
    <li>
      <label>
        <input type="checkbox" />
        <span>吃飯</span>
      </label>
      <button>删除</button>
    </li>

    <li>
      <label>
        <input type="checkbox" />
        <span>睡覺</span>
      </label>
      <button>删除</button>
    </li>

    <li>
      <label>
        <input type="checkbox" />
        <span>打豆豆</span>
      </label>
      <button>删除</button>
    </li>
  </div>
</template>

<script>export default {
  name: "MyItem",
  data() {},

  methods: {},
};</script>

<style scoped>/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;

  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

li:hover {
  background-color: #ddd;
}

li:hover button {
  display: block;
}</style>      

4.4 TheList.vue

<template>
  <ul class="todo-main">
    <TheItem />
  </ul>
</template>

<script>import TheItem from "./TheItem";

export default {
  name: "TheList",
  components: { TheItem },

};</script>

<style scoped>/*main*/
.todo-main {
  margin-left: 0px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding: 0px;
}

.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}</style>      

4.5 App.vue

<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <TheHeader />
        <TheList />
        <TheFooter />
      </div>
    </div>
  </div>
</template>

<script>import TheHeader from "./components/TheHeader";
import TheList from "./components/TheList";
import TheFooter from "./components/TheFooter.vue";

export default {
  name: "App",
  components: { TheHeader, TheList, TheFooter },
  data() {
    return {};
  },
  methods: {},
};</script>

<style>/*base*/
body {
  background: #fff;
}
.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}
.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn:focus {
  outline: none;
}
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}</style>      

4.6 main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'



Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})      

5、靜态頁面效果(功能還未添加)

繼續閱讀