vue.js - Computed properties on function instances -
in knockout.js able following:
function person() { this.firstname = ko.observable('foo') this.lastname = ko.observable('bar') this.fullname = ko.purecomputed(() => { return this.firstname() + this.lastname() }) } var p = new person() console.log(p.fullname()) // foobar
is there way add reactive computed properties on objects which not component data in vue?
in vue, not need explicitly declare reactive properties , methods knockout requires. plain object.
what makes object reactive in vue when assign component through property declared in data
object of component.
so example in vue be:
function person() { this.firstname = 'foo'; this.lastname = 'bar'; this.fullname = function () { return this.firstname + this.lastname; }; } var p = new person(); console.log(p.fullname()); // foobar
if use inside component, reactive, this:
const comp = new vue({ template: '<div>{{ person.fullname() }}</div>', data: { person: null, }, }); // vue detects p being assigned reactive property // 'person' , makes p reactive. comp.person = p;
i should mention, i'm not sure mean "which not component data in vue" because whole reason why want reactive in first place because want rendered in component's template , component re-render automatically when property changes, can't make reactive externally vue.
i understood question along lines of "how make reactive vue without needing specify directly within vue component's data or computed properties?". keep in mind person
property must declared upfront in component's data
property (and data assigned it) reactive. long that, can design model objects want.
wiki
Comments
Post a Comment