Program main
Test text and component
Vuex.store
...
Name: Guest
function TestFiter(vue, options) {
vue.config.globalProperties.$filters = vue.config.globalProperties.$filters || {};
vue.config.globalProperties.$filters.capitalize = function(value) {
if (!value) return '';
value = value.toString();
return value.charAt(0).toUpperCase() + value.slice(1);
}
}
function TestDirective(vue, options) {
vue.directive('example', {
// called before bound element's attributes or event listeners are applied
created(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode);
el.setAttribute('disabled', 'disabled');
},
// called right before the element is inserted into the DOM.
beforeMount() {},
// called when the bound element's parent component and all its children are mounted.
mounted() {},
// called before the parent component is updated
beforeUpdate() {},
// called after the parent component and all of its children have updated
updated() {},
// called before the parent component is unmounted
beforeUnmount() {},
// called when the parent component is unmounted
unmounted() {}
});
}
function ButonCounter(vue, options) {
vue.component('button-counter', {
props: {
onClick: {
type: Function,
default() {}
}
},
data() {
return {
clicks: 0,
}
},
template: ``,
methods: {
onButttonClick($event) {
this.clicks += 1;
this.$emit('onClick', this.clicks);
}
},
});
}
const App = {
data() {
return {
name: "Test",
}
},
computed: {
theState() {
return this.$store.state;
},
nameDyn() {
return this.$filters.capitalize(this.$store?.state?.user?.login || "Гость");
}
}
};
const GET_USER = "act384";
const store = Vuex.createStore({
state: () => ({
count: 1,
user: null
}),
mutations: {
increment (state) {
state.count++
},
user(state, user) {
state.user = user
}
},
actions: {
increment (context) {
context.commit('increment')
},
[GET_USER](context, {login}) {
fetch(`https://api.github.com/users/${login}`)
.then(response => response.json())
.then(response => {
context.commit('user', response);
})
.catch(err => {
context.commit('user', null);
})
},
}
});
async function main() {
const inst = Vue.createApp(App);
inst.use(store);
inst.use(ButonCounter);
inst.use(TestDirective);
inst.use(TestFiter);
inst.mount(".container");
}
main().catch(console.error);
CDNS
* https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.37/vue.global.prod.min.js
* https://cdnjs.cloudflare.com/ajax/libs/vuex/4.0.2/vuex.global.prod.min.js