Vue.jsを使用する際の注意点は何ですか?

php中世界最好的语言
リリース: 2018-03-13 13:55:37
オリジナル
1770 人が閲覧しました

今回は、Vue.jsを使用する際の注意事項についてお届けします。以下は実際のケースです。

1. パラメータを渡すときは、2 番目のパラメータとその前のカンマの間にスペースが必要です

window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items))
ログイン後にコピー

2. スペースに注意してください

正しい形式

<script>import Store from &#39;./store&#39;console.log(Store)export default {   ... }</script>
错误格式
<script>  import Store from &#39;./store&#39;  console.log(Store)export default {   ... }</script>
ログイン後にコピー

3. 親はパラメータを子コンポーネントに渡します

コンポーネント

//模板中<template>
  <div id="app">
    //之前老版本  <conponent-a msgfromfather="父亲传给儿子!"></conponent-a>
    <ConponentA msgfromfather="父亲传给儿子!"></ConponentA>
  </div></template>//Js<script>export default {  //注册ConponentA
  components: {ConponentA},
}</script>
ログイン後にコピー

子コンポーネント内で

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button v-on:click="onClickMe()">点我啊,小样儿</button>
  </div></template><script>
  export default {
    data () {      return {        msg: &#39;hello from component A!&#39;
      }
    },    //props 可以是数组或对象,用于接收来自父组件的数据
    props: [&#39;msgfromfather&#39;],    methods: {      onClickMe: function () {         //打印从父组件传过来的值
        console.log(this.msgfromfather)
      }
    }
  }</script><style scoped>
  h1 {    font-weight: normal;
  }</style>
ログイン後にコピー

4. 息子は親コンポーネントにパラメータを渡します

息子は、イベントをトリガーしてイベントをリッスンするために vm.$emit と vm.$on を使用する必要があることを父親に伝えます

子コンポーネント内

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h1>{{msgfromfather}}</h1>
    <button v-on:click="onClickMe()">点我啊,小样儿</button>
  </div></template><script>
  export default {
    data () {      return {        msg: &#39;hello from component A!&#39;
      }
    },    methods: {      onClickMe: function () {//        子传父 触发当前实例上的事件
        this.$emit(&#39;child-tell-me-something&#39;, this.msg)
      }
    }
  }</script><style scoped>
  h1 {    font-weight: normal;
  }</style>
ログイン後にコピー

親コンポーネント内

<template>
  <div id="app">
    <p>child tells me: {{childWorlds}}</p>
    <ConponentA msgfromfather="父亲传给儿子!" v-on:child-tell-me-something="listenToMyBoy"></ConponentA>
  </div></template><script>import ConponentA from &#39;./components/componentA.vue&#39;export default {  data: function () {    return {      childWorlds: &#39;&#39;
    }
  },  components: {ConponentA},  watch: {    items: {      handler: function (items) {
        Store.save(items)
      },      deep: true
    }
  },  methods: {    //监听
    listenToMyBoy: function (msg) {      console.log(msg)      this.childWorlds = msg
    }
  }
}</script>
ログイン後にコピー

Except このメソッド以外にもメソッドはあります、詳しくはVue.js公式サイトをご覧ください

自己定義コンポーネント属性を指定するデータ型

export default {  props: {    slides: {      type: Array,     //数组      default: []      //默认值    }  },
在加载完毕执行某个方法
 mounted () {    this.loadxxx()  }
ログイン後にコピー

@mouseover="xxxx" マウス入力(イベント実行)、@mouseout="xxxx" マウスアウト(イベント実行)

アニメーション効果を実現したい場合は、 x 軸のみを使用できます。

slot スロット

this.abc = false は this ['abc'] = false と同等です

親コンポーネントのスタイルはスコープを追加しないので、その子コンポーネントはそのスタイルを共有できます。 style、つまり、子コンポーネントのスタイルを親コンポーネントに記述することができます。

<!-- Add "scoped" attribute to limit CSS to this component only --><style>......</style>
ログイン後にコピー

&は親を表します要素

<!--css书写规范 可被继承的写在前面,不让继承的的写在后面--><style lang="stylus" rel="stylesheet/stylus">
  #app
    .tab
      display: flex
      width: 100%      height: 40px
      line-height: 40px
      .tab-item
        flex: 1        text-align: center
        /* & > a & 代表父元素 tab-item 子元素选择器 */
        & > a
          display: block
          font-style: 14px
          color: rgb(77,85,93)
          &.active
            color: rgb(240,20,20)</style>
ログイン後にコピー

1ピクセルボーダー

の実装は、次のようにしてPC側で実現できます設定、

border-bottom: 1px solid rgba(7,17,27,0.1)
ログイン後にコピー
この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、PHP 中国語 Web サイトの他の関連記事に注目してください。

推奨書籍:

JavaScript での JS の動きを深く理解する

JavaScript での DOM の高度なアプリケーションを詳細に理解する

JavaScript のちょっとした知識ポイントを理解する

以上がVue.jsを使用する際の注意点は何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!