Skip to content

Commit

Permalink
🐛 bug(mixin): fix computed props errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 17, 2017
1 parent dc978dd commit a6b7e37
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,52 @@
import VueI18n from './index'
import { isPlainObject, warn } from './util'

export default {
computed: {
$t (): Function {
if (!this.$i18n) {
throw Error(`Failed in $t due to not find VueI18n instance`)
}
// add dependency tracking !!
const locale: Locale = this.$i18n.locale
const messages: LocaleMessages = this.$i18n.messages
return (key: string, ...args: any): TranslateResult => {
return this.$i18n._t(key, locale, messages, this, ...args)
}
},

$tc (): Function {
if (!this.$i18n) {
throw Error(`Failed in $tc due to not find VueI18n instance`)
}
// add dependency tracking !!
const locale: Locale = this.$i18n.locale
const messages: LocaleMessages = this.$i18n.messages
return (key: string, choice?: number, ...args: any): TranslateResult => {
return this.$i18n._tc(key, locale, messages, this, choice, ...args)
}
},
const $t = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const messages: LocaleMessages = vm.$i18n.messages
return (key: string, ...args: any): TranslateResult => {
return vm.$i18n._t(key, locale, messages, vm, ...args)
}
}
const $tc = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const messages: LocaleMessages = vm.$i18n.messages
return (key: string, choice?: number, ...args: any): TranslateResult => {
return vm.$i18n._tc(key, locale, messages, vm, choice, ...args)
}
}
const $te = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const messages: LocaleMessages = vm.$i18n.messages
return (key: string, ...args: any): boolean => {
return vm.$i18n._te(key, locale, messages, ...args)
}
}

$te (): Function {
if (!this.$i18n) {
throw Error(`Failed in $te due to not find VueI18n instance`)
}
// add dependency tracking !!
const locale: Locale = this.$i18n.locale
const messages: LocaleMessages = this.$i18n.messages
return (key: string, ...args: any): boolean => {
return this.$i18n._te(key, locale, messages, ...args)
}
}
},
function defineComputed (vm: any, options: any): void {
options.computed = options.computed || {}
options.computed.$t = () => $t(vm)
options.computed.$tc = () => $tc(vm)
options.computed.$te = () => $te(vm)
}

export default {
beforeCreate (): void {
const options: any = this.$options
if (options.i18n) {
if (options.i18n instanceof VueI18n) {
this.$i18n = options.i18n
defineComputed(this, options)
} else if (isPlainObject(options.i18n)) {
// component local i18n
if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
options.i18n.root = this.$root.$i18n
}
this.$i18n = new VueI18n(options.i18n)
defineComputed(this, options)
if (options.i18n.sync) {
this._localeWatcher = this.$i18n.watchLocale()
}
Expand All @@ -64,10 +60,13 @@ export default {
} else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
// root i18n
this.$i18n = this.$root.$i18n
defineComputed(this, options)
}
},

destroyed (): void {
beforeDestroy (): void {
if (!this.$i18n) { return }

if (this._localeWatcher) {
this.$i18n.unwatchLocale()
delete this._localeWatcher
Expand Down

0 comments on commit a6b7e37

Please sign in to comment.