mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
example: Lint & fix TS example
This commit is contained in:
parent
aa62024cb4
commit
dd83c87f64
@ -1,26 +1,28 @@
|
||||
<template>
|
||||
<div class="ba b--black-20 mw5 ma2">
|
||||
<img :src="'https://robots.johnlindquist.com/' + person.first_name + '_' + person.last_name" />
|
||||
<div class="flex flex-column items-center pa2 b--black-20">
|
||||
<div class="f4">{{person.first_name}} {{person.last_name}}</div>
|
||||
<button @click="select(person.id)" class="w-100 bg-blue dim mv2 pv2 bn pointer">Select</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
// **PLEASE NOTE** All "Nuxt Class Components" require at minimum a script tag that exports a default object
|
||||
<script lang="ts">
|
||||
import Vue from 'vue'
|
||||
import Component from 'nuxt-class-component'
|
||||
import { Prop } from 'vue-property-decorator'
|
||||
import { Action, namespace } from 'vuex-class'
|
||||
|
||||
import * as people from '~/store/modules/people';
|
||||
|
||||
const PeopleAction = namespace(people.name, Action);
|
||||
|
||||
@Component({})
|
||||
export default class Card extends Vue {
|
||||
@Prop() person
|
||||
@PeopleAction select
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<div class="ba b--black-20 mw5 ma2">
|
||||
<img :src="'https://robots.johnlindquist.com/' + person.first_name + '_' + person.last_name" />
|
||||
<div class="flex flex-column items-center pa2 b--black-20">
|
||||
<div class="f4">{{person.first_name}} {{person.last_name}}</div>
|
||||
<button @click="select(person.id)" class="w-100 bg-blue dim mv2 pv2 bn pointer">Select</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
// PLEASE NOTE
|
||||
// All "Nuxt Class Components" require at minimum a script tag that exports a default object
|
||||
import Vue from 'vue'
|
||||
import Component from 'nuxt-class-component'
|
||||
import { Prop } from 'vue-property-decorator'
|
||||
import { Action, namespace } from 'vuex-class'
|
||||
|
||||
import * as people from '~/store/modules/people'
|
||||
|
||||
const PeopleAction = namespace(people.name, Action)
|
||||
|
||||
@Component({})
|
||||
export default class Card extends Vue {
|
||||
@Prop() person
|
||||
@PeopleAction select
|
||||
}
|
||||
</script>
|
||||
|
@ -1,4 +1,6 @@
|
||||
module.exports = function (options) {
|
||||
// Add .ts extension for store, middleware and more
|
||||
this.nuxt.options.extensions.push('ts')
|
||||
// Extend build
|
||||
this.extendBuild(config => {
|
||||
const tsLoader = {
|
||||
@ -22,5 +24,9 @@ module.exports = function (options) {
|
||||
rule.options.loaders.ts = tsLoader
|
||||
}
|
||||
}
|
||||
// Add .ts extension in webpack resolve
|
||||
if (config.resolve.extensions.indexOf('.ts') === -1) {
|
||||
config.resolve.extensions.push('.ts')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ module.exports = {
|
||||
*/
|
||||
css: ['tachyons/css/tachyons.min.css', '~/assets/css/main.css'],
|
||||
build: {
|
||||
vendor: ['axios', 'gsap', 'vuex-class', 'nuxt-class-component']
|
||||
vendor: ['axios', 'vuex-class', 'nuxt-class-component']
|
||||
},
|
||||
modules: ['~/modules/typescript']
|
||||
}
|
||||
|
@ -2,15 +2,10 @@
|
||||
"version": "1.0.1",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"axios": "^0.16.1",
|
||||
"gsap": "^1.19.1",
|
||||
"axios": "^0.17.1",
|
||||
"nuxt": "latest",
|
||||
"nuxt-class-component": "^1.0.3",
|
||||
"tachyons": "^4.7.0",
|
||||
"vue": "~2.5.1",
|
||||
"vue-server-renderer": "~2.5.1",
|
||||
"vue-template-compiler": "~2.5.1",
|
||||
"vue-class-component": "^6.0.0",
|
||||
"nuxt-class-component": "^1.1.3",
|
||||
"tachyons": "^4.9.1",
|
||||
"vue-property-decorator": "^6.0.0",
|
||||
"vuex-class": "^0.3.0"
|
||||
},
|
||||
@ -21,7 +16,7 @@
|
||||
"generate": "nuxt generate"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts-loader": "^3.0.0",
|
||||
"typescript": "^2.2.2"
|
||||
"ts-loader": "^3.2.0",
|
||||
"typescript": "^2.6.2"
|
||||
}
|
||||
}
|
||||
|
@ -19,10 +19,10 @@ import Component from 'nuxt-class-component'
|
||||
import Card from '~/components/Card.vue'
|
||||
import { State, Getter, namespace } from 'vuex-class'
|
||||
|
||||
import * as people from '~/store/modules/people';
|
||||
import * as people from '~/store/modules/people'
|
||||
|
||||
const PeopleState = namespace(people.name, State);
|
||||
const PeopleGetter = namespace(people.name, Getter);
|
||||
const PeopleState = namespace(people.name, State)
|
||||
const PeopleGetter = namespace(people.name, Getter)
|
||||
|
||||
@Component({
|
||||
components: {
|
||||
|
@ -1,25 +1,32 @@
|
||||
import * as root from "./root";
|
||||
import * as people from "./modules/people";
|
||||
import Vuex from 'vuex'
|
||||
import * as root from './root'
|
||||
import * as people from './modules/people'
|
||||
|
||||
// more info about store: https://vuex.vuejs.org/en/core-concepts.html
|
||||
// More info about store: https://vuex.vuejs.org/en/core-concepts.html
|
||||
// See https://nuxtjs.org/guide/vuex-store#classic-mode
|
||||
// structure of the store:
|
||||
// types: Types that represent the keys of the mutations to commit
|
||||
// state: The information of our app, we can get or update it.
|
||||
// getters: Get complex information from state
|
||||
// action: Sync or async operations that commit mutations
|
||||
// mutations: Modify the state
|
||||
|
||||
export const modules = {
|
||||
[people.name]: people
|
||||
};
|
||||
// types: Types that represent the keys of the mutations to commit
|
||||
// state: The information of our app, we can get or update it.
|
||||
// getters: Get complex information from state
|
||||
// action: Sync or async operations that commit mutations
|
||||
// mutations: Modify the state
|
||||
|
||||
interface ModulesStates {
|
||||
people: people.State;
|
||||
people: people.State
|
||||
}
|
||||
|
||||
export type RootState = root.State & ModulesStates;
|
||||
export type RootState = root.State & ModulesStates
|
||||
|
||||
export const state = root.state;
|
||||
export const getters = root.getters;
|
||||
export const actions = root.actions;
|
||||
export const mutations = root.mutations;
|
||||
const createStore = () => {
|
||||
return new Vuex.Store({
|
||||
state: root.state(),
|
||||
getters: root.getters,
|
||||
mutations: root.mutations,
|
||||
actions: root.actions,
|
||||
modules: {
|
||||
[people.name]: people
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default createStore
|
||||
|
@ -1,70 +1,71 @@
|
||||
import { ActionTree, MutationTree, GetterTree, ActionContext } from "vuex";
|
||||
import { RootState } from "store";
|
||||
import { ActionTree, MutationTree, GetterTree, ActionContext } from 'vuex'
|
||||
import { RootState } from 'store'
|
||||
|
||||
export const name = "people";
|
||||
export const name = 'people'
|
||||
|
||||
export const types = {
|
||||
SELECT: "SELECT",
|
||||
SET: "SET"
|
||||
};
|
||||
SELECT: 'SELECT',
|
||||
SET: 'SET'
|
||||
}
|
||||
|
||||
export interface PersonContact {
|
||||
email: string;
|
||||
phone: string;
|
||||
email: string
|
||||
phone: string
|
||||
}
|
||||
|
||||
export interface PersonAddress {
|
||||
city: string;
|
||||
country: string;
|
||||
postalCode: string;
|
||||
state: string;
|
||||
street: string;
|
||||
city: string
|
||||
country: string
|
||||
postalCode: string
|
||||
state: string
|
||||
street: string
|
||||
}
|
||||
|
||||
export interface Person {
|
||||
id: number;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
contact: PersonContact;
|
||||
gender: string;
|
||||
ip_address: string;
|
||||
avatar: string;
|
||||
addres: PersonAddress;
|
||||
id: number
|
||||
first_name: string
|
||||
last_name: string
|
||||
contact: PersonContact
|
||||
gender: string
|
||||
ip_address: string
|
||||
avatar: string
|
||||
addres: PersonAddress
|
||||
}
|
||||
|
||||
export interface State {
|
||||
selected: number;
|
||||
people: Person[];
|
||||
selected: number
|
||||
people: Person[]
|
||||
}
|
||||
|
||||
export const namespaced = true
|
||||
|
||||
export const state = (): State => ({
|
||||
selected: 1,
|
||||
people: []
|
||||
});
|
||||
selected: 1,
|
||||
people: []
|
||||
})
|
||||
|
||||
export const getters: GetterTree<State, RootState> = {
|
||||
selectedPerson: state => {
|
||||
const p = state.people.find(person => person.id === state.selected);
|
||||
return p ? p : { first_name: "Please,", last_name: "select someone" };
|
||||
}
|
||||
};
|
||||
selectedPerson: state => {
|
||||
const p = state.people.find(person => person.id === state.selected)
|
||||
return p ? p : { first_name: 'Please,', last_name: 'select someone' }
|
||||
}
|
||||
}
|
||||
|
||||
export interface Actions<S, R> extends ActionTree<S, R> {
|
||||
select(context: ActionContext<S, R>, id: number): void;
|
||||
select(context: ActionContext<S, R>, id: number): void
|
||||
}
|
||||
|
||||
export const actions: Actions<State, RootState> = {
|
||||
select({ commit }, id: number) {
|
||||
commit(types.SELECT, id);
|
||||
}
|
||||
};
|
||||
select({ commit }, id: number) {
|
||||
commit(types.SELECT, id)
|
||||
}
|
||||
}
|
||||
|
||||
export const mutations: MutationTree<State> = {
|
||||
[types.SELECT](state, id: number) {
|
||||
state.selected = id;
|
||||
},
|
||||
[types.SET](state, people: Person[]) {
|
||||
state.people = people;
|
||||
}
|
||||
};
|
||||
|
||||
[types.SELECT](state, id: number) {
|
||||
state.selected = id
|
||||
},
|
||||
[types.SET](state, people: Person[]) {
|
||||
state.people = people
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,26 @@
|
||||
import { GetterTree, ActionContext, ActionTree, MutationTree } from "vuex";
|
||||
import axios from "~/plugins/axios";
|
||||
import { RootState } from "store";
|
||||
import * as people from "./modules/people";
|
||||
import { GetterTree, ActionContext, ActionTree, MutationTree } from 'vuex'
|
||||
import axios from '~/plugins/axios'
|
||||
import { RootState } from 'store'
|
||||
import * as people from './modules/people'
|
||||
|
||||
export const types = {};
|
||||
export const types = {}
|
||||
|
||||
export interface State {}
|
||||
|
||||
export const state = (): State => ({});
|
||||
export const state = (): State => ({})
|
||||
|
||||
export const getters: GetterTree<State, RootState> = {};
|
||||
export const getters: GetterTree<State, RootState> = {}
|
||||
|
||||
export interface Actions<S, R> extends ActionTree<S, R> {
|
||||
nuxtServerInit(context: ActionContext<S, R>): void;
|
||||
nuxtServerInit(context: ActionContext<S, R>): void
|
||||
}
|
||||
|
||||
export const actions: Actions<State, RootState> = {
|
||||
async nuxtServerInit({ commit }) {
|
||||
const response = await axios.get("/random-data.json");
|
||||
const staticPeople = response.data.slice(0, 10);
|
||||
commit(`${people.name}/${people.types.SET}`, staticPeople, { root: true });
|
||||
}
|
||||
};
|
||||
|
||||
export const mutations: MutationTree<State> = {};
|
||||
|
||||
async nuxtServerInit({ commit }) {
|
||||
const response = await axios.get('/random-data.json')
|
||||
const staticPeople = response.data.slice(0, 10)
|
||||
commit(`${people.name}/${people.types.SET}`, staticPeople, { root: true })
|
||||
}
|
||||
}
|
||||
|
||||
export const mutations: MutationTree<State> = {}
|
||||
|
Loading…
Reference in New Issue
Block a user