te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Watch for data change - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Watch for data change - Stack Overflow

programmeradmin3浏览0评论

I'm currently struggling with the Vue instance concept. I'm using the vue-loader with webpack and created a vue ponent that I'm importing.

import Vue from 'vue';
import Search from '../Search.vue';
const vm = new Vue({
   el: 'search',
   render: h => h(search)
});

On the ponent:

export default {
    data: function() {
        return {
            results: 'current results'
        }
    }
}

I want trigger and event from inside of the ponent (on a property change) that I can watch from the instance.

Using the API should be something like this:

vm.$watch('results', function (newVal, oldVal) {
  // do something
})

But I does not trigger anything.

I think I'm missing to understand how to use the Vue instance because I can't also add any data on initialization of the instance that is accessible on the ponent like:

const vm = new Vue({
   el: 'search',
   data: {index: 1},
   render: h => h(search)
});

Index is also not available inside of the ponent to use.

How do I fix this?

I'm currently struggling with the Vue instance concept. I'm using the vue-loader with webpack and created a vue ponent that I'm importing.

import Vue from 'vue';
import Search from '../Search.vue';
const vm = new Vue({
   el: 'search',
   render: h => h(search)
});

On the ponent:

export default {
    data: function() {
        return {
            results: 'current results'
        }
    }
}

I want trigger and event from inside of the ponent (on a property change) that I can watch from the instance.

Using the API should be something like this:

vm.$watch('results', function (newVal, oldVal) {
  // do something
})

But I does not trigger anything.

I think I'm missing to understand how to use the Vue instance because I can't also add any data on initialization of the instance that is accessible on the ponent like:

const vm = new Vue({
   el: 'search',
   data: {index: 1},
   render: h => h(search)
});

Index is also not available inside of the ponent to use.

How do I fix this?

Share Improve this question edited Sep 5, 2017 at 8:35 jpsc asked Sep 5, 2017 at 7:36 jpscjpsc 511 gold badge1 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

You can easily use watchers here. But first, let me show you how you can use data inside of your ponents. So basically, if you define data in Vue instances, you have them available there. But you want to pass them to your ponents. Here's where props e in handy.

https://v2.vuejs/v2/guide/ponents.html#Props

Basically, the important part here is that you have a child like yours...

export default {
  data: function() {
    return {
        results: 'current results'
    }
  }
}

... and you want to make some kind of value available to use inside it, which is supposed to e from outside. So, you can do this by defining what you expect to be given towards your ponent by addingprops like so:

export default {
  props: ['index'],
  data: function() {
    return {
        results: 'current results'
    }
  }
}

Now, you just have to pass it to your ponent whenever you use it. Let's say you give your ponent a name attribute like this: name: test-element and your ponent sits in an own file called TestElement.vue - obviously you can change that to whatever you want, but let's stick with it for now.

Then you can import your ponent inside of your parent with import TestElement from './TestElement.vue' - again, your path may be different!

Now, you register your ponent in your Vue instance by using ponents: {'test-element': TestElement}.

This enables you to use an HTML tag <test-element> inside your current file which will then render whatever you defined inside of your ponent!

Now you just bind your value to the prop which is then being passed to your ponent like so: <test-element :index="index"></test-element>

And that is it! Now the value of index defined in your parent's Vue instance is being passed to your child ponent where you can use it, for example in your template, to do calculations, whatever you want!

Now your question about watchers: inside of your ponent, register a watcher like so...

export default {
  props: ['index'],
  data: function() {
    return {
      results: 'current results',
      index: this.index // here we are using your prop!
    }
  },
  watch: {
    index: function (newValue, oldValue) {
      // here you can do whatever you want
    }
  }
}

So that is basically it! Sorry if this was a little long, but I hope I could help you ;)

This is actually what I had to do.

const Search = Vue.extend(Search);
const vm = new Search({el: 'search'});

vm.$watch('results', function (newVal, oldVal) {
  // do something
})

Now I can watch for changes on the properties from outside of the .vue file. This was an issue because I have a mix of .js and prepile .vue with vue-loader.

发布评论

评论列表(0)

  1. 暂无评论