成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Tricks in Vue

Ali_ / 2918人閱讀

There are some tricks which can"t be found easily in Vue.js homepage. So, for convenient, I summarized them here.

Vue Access Global Variable in Template

Have you ever done something like this in lots of components?



or



Actually, you don"t have to, you can register window or bus in Vue.prototype like:

Vue.prototype.window = window
Vue.prototype.bus = bus

in the main.js or the entry file. Then you can use bus or window in template directly. Also, this usage prevents Vue watching the attributes of bus or window which would bring a waste of performance.

Reactive or Not Reactive

Always, if we want a data reactive, we have to do something like this:

data: {
  newTodoText: "",
  visitCount: 0,
  hideCompletedTodos: false,
  todos: [],
  error: null
}

Set some initial value to adds all the properties found in its data object to Vue"s reactivity system.

Things we need to take care about is:

If we want to add reactive attributes dynamically, we have to use something like Vue.set or this.$set. Otherwise, they might not be reactive.

If we definitely don"t want some data to participate in Vue"s reactivity system even we initialize it in data. We can use something like Object.freeze(). For example, freeze a huge array to improve performance.

Scoped Style Won"t Work on Dynamically Inserted Elements

I always use the

color: red won"t work on .App__title because of scoped. The actual style is rendered with a unique attribute like:

So, how do we solve this? /deep/ or >>>.

They can be used to override child component style. Here is the doc.

Smarter Watchers

Have you ever written code like this:

{
  // ...
  created() {
    this.fetchPostList()
  },
  watch: {
    searchInputValue() {
      this.fetchPostList()
    }
  }
  // ...
}

Actually, you can simplify it by

{
  // ...
  watch: {
    searchInputValue:{
      handler: "fetchPostList",
      immediate: true
    }
  }
  // ...
}

As the doc said:

Passing in immediate: true in the option will trigger the callback immediately with the current value of the expression.
$attrs and $listeners

I don"t know if you have used $attrs and $listeners from this. However, I never used those until I met this situation. For example:

It"s obviously tedious to bind every attribute and listener by hand. Actually, this is where $attrs and $listeners will help us. We can write the BaseInput template like:

let BaseInput = {
  name: "base-input",
  template: `
`, props: { value: { type: String } }, computed: { listeners() { const listeners = { ...this.$listeners, // move `focus` in to `listeners` instead of adding one more `focus` listener. focus: this.focusCb } return listeners } }, methods: { focusCb(event) { console.log("child", event) } } }
Vue-Router $router and $route

Have you ever wonder about the relationship between $router and $route? I give you a hint:

this.$router.currentRoute === this.$route //true
Vuex Commit Data by One Mutation

We can"t directly mutate state in Vuex. Instead, we have to commit a mutation to mutate the data. However, it would be tedious to write lots of similar mutations like this:

let store = new Vuex.Store({
  modules: {
    // ...
  },
  mutations: {
    updateName(state, data) {
      state.name = data
    },
    updateChildrenCount(state, data) {
      state.children.count = data
    }
    // other similar mutations
  }
})

We can write a public mutation to do this like:

let store = new Vuex.Store({
  modules: {
    // ...
  },
  mutations: {
    replaceProperty(state, { path, data }) {
      if (typeof path !== "string") {
        return
      }
      path = path.split(".")
      let targetObj = path.slice(0, -1).reduce((re, key) => re[key], state)
      targetObj[path.pop()] = data
    }
  }
})

Then we can mutate state in anywhere with only one mutation like:

commit(
  "replaceProperty",
  {
    path: "name",
    data: name
  },
  { root: true }
)
commit(
  "replaceProperty",
  {
    path: "children.count",
    data: data
  },
  { root: true }
)
commit(
  "replaceProperty",
  {
    path: "some.other.deep.path.in.state",
    data: data
  },
  { root: true }
)

It would also work for modules!

Original Post

Reference

7 Secret Patterns Vue Consultants Don’t Want You to Know - Chris Fritz

vue-loader/issues/749

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/108875.html

相關(guān)文章

  • 如何理解debounce和throttle?

    摘要:前端工程師們都聽過看起來很高級的詞,節(jié)流和防抖,其實(shí)節(jié)流就是,防抖就是,其實(shí)這個(gè)也屬于前端性能優(yōu)化的一部分。具體就不寫了,因?yàn)槌S糜谶B續(xù)事件的事件處理函數(shù)??梢詤⒖嘉恼伦詈蟮?,其中的在上的運(yùn)用,就是的正確打開方式。 showImg(https://segmentfault.com/img/bVbmYxW?w=960&h=540); 前端工程師們都聽過看起來很高級的詞,節(jié)流和防抖,其實(shí)節(jié)...

    CoderStudy 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<