Skip to content

Commit

Permalink
refactor: Vue3 组合式 API 写法重构 (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
1004801012 authored and sudoooooo committed May 24, 2024
1 parent de7344b commit 89d416b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 126 deletions.
23 changes: 6 additions & 17 deletions web/src/management/pages/create/CreatePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,15 @@
</div>
</template>

<script>
<script setup lang="ts">
import { ref } from 'vue'
import TypeList from './components/TypeList.vue'
import CreateForm from './components/CreateForm.vue'
export default {
name: 'CreatePage',
components: {
TypeList,
CreateForm
},
data() {
return {
selectType: 'normal'
}
},
methods: {
onSelectTypeChange(selectType) {
this.selectType = selectType
}
}
const selectType = ref<string>('normal')
const onSelectTypeChange = (type: string) => {
selectType.value = type
}
</script>

Expand Down
119 changes: 59 additions & 60 deletions web/src/management/pages/create/components/CreateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,78 +35,77 @@
</div>
</template>

<script>
<script setup lang="ts">
import { ref, reactive, computed, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/src/message.scss'
import { createSurvey } from '@/management/api/survey'
import { SURVEY_TYPE_LIST } from '../types'
export default {
name: 'CreateForm',
props: {
selectType: {
type: String,
default: 'normal'
}
},
data() {
return {
rules: {
title: [{ required: true, message: '请输入问卷标题', trigger: 'blur' }]
},
canSubmit: true,
form: {
title: '问卷调研',
remark: '问卷调研'
}
}
interface Props {
selectType?: string
}
const props = withDefaults(defineProps<Props>(), {
selectType: 'normal'
})
const ruleForm = ref<any>(null)
const state = reactive({
rules: {
title: [{ required: true, message: '请输入问卷标题', trigger: 'blur' }]
},
computed: {
SURVEY_TYPE_LIST() {
return SURVEY_TYPE_LIST
},
title() {
return this.SURVEY_TYPE_LIST.find((item) => item.type === this.selectType)?.title
canSubmit: true,
form: {
title: '问卷调研',
remark: '问卷调研'
}
})
const { rules, canSubmit, form } = toRefs(state)
const title = computed(() => {
return SURVEY_TYPE_LIST.find((item) => item.type === props.selectType)?.title
})
const checkForm = (fn: Function) => {
ruleForm.value?.validate?.((valid: boolean) => {
valid && typeof fn === 'function' && fn()
})
}
const router = useRouter()
const submit = () => {
if (!state.canSubmit) {
return
}
checkForm(async () => {
const { selectType } = props
if (!state.canSubmit) {
return
}
},
methods: {
checkForm(fn) {
this.$refs.ruleForm.validate((valid) => {
valid && typeof fn === 'function' && fn()
})
},
submit() {
if (!this.canSubmit) {
return
}
this.checkForm(async () => {
const { selectType } = this
if (!this.canSubmit) {
return
}
this.canSubmit = false
const res = await createSurvey({
surveyType: selectType,
...this.form
})
if (res.code === 200 && res?.data?.id) {
const id = res.data.id
this.$router.push({
name: 'QuestionEditIndex',
params: {
id
}
})
} else {
ElMessage.error(res.errmsg || '创建失败')
state.canSubmit = false
const res:any = await createSurvey({
surveyType: selectType,
...state.form
})
if (res?.code === 200 && res?.data?.id) {
const id = res.data.id
router.push({
name: 'QuestionEditIndex',
params: {
id
}
this.canSubmit = true
})
} else {
ElMessage.error(res?.errmsg || '创建失败')
}
}
state.canSubmit = true
})
}
</script>

Expand Down
49 changes: 24 additions & 25 deletions web/src/management/pages/create/components/NavHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@
<div :class="['nav-header', `${boxShadow ? 'box-shadow' : null}`]">
<div class="left">
<div class="logo">
<img class="logoImg" :src="img" alt @click="toHomePage" />
<img class="logoImg" :src="img" @click="toHomePage" />
</div>
<div class="return no-logo-return icon-fanhui" @click="onBack">返回</div>
</div>
</div>
</template>

<script>
export default {
props: {
boxShadow: {
type: Boolean,
default: true
}
},
name: 'NavHeader',
data() {
return {
img: '/imgs/s-logo.webp'
}
},
methods: {
toHomePage() {
this.$router.replace({
name: 'survey'
})
},
onBack() {
this.$router.go(-1)
}
}
<script setup lang="ts">
import { useRouter } from 'vue-router'
interface Props {
boxShadow?: boolean
}
withDefaults(defineProps<Props>(), {
boxShadow: true
})
const img = '/imgs/s-logo.webp'
const router = useRouter()
const onBack = () => {
router.go(-1)
}
const toHomePage = () => {
router.push({
name: 'Home'
})
}
</script>

Expand Down
41 changes: 17 additions & 24 deletions web/src/management/pages/create/components/TypeList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
v-for="item in renderData"
class="list-item"
:key="item.type"
@click="handleSelectType('selectType', item)"
@click="handleSelectType(item.type)"
>
<div class="selected-border" v-if="selectType === item.type" />
<img class="img" :src="item.img" alt="类别图片" />
Expand All @@ -24,34 +24,27 @@
</div>
</template>

<script>
<script setup lang="ts">
import NavHeader from './NavHeader.vue'
import { SURVEY_TYPE_LIST } from '../types'
import { computed } from 'vue'
export default {
name: 'LeftSide',
components: {
NavHeader
},
props: {
selectType: {
type: String,
default: ''
}
},
computed: {
renderData() {
return SURVEY_TYPE_LIST
}
},
methods: {
handleSelectType(key, value) {
const { type } = value
this.$emit('selectTypeChange', type)
}
}
interface Props {
selectType?: string
}
withDefaults(defineProps<Props>(), {
selectType: ''
})
const emit = defineEmits(['selectTypeChange'])
const handleSelectType = (selectType: string) => {
emit('selectTypeChange', selectType)
}
const renderData = computed(() => SURVEY_TYPE_LIST)
</script>

<style lang="scss" scoped>
Expand Down

0 comments on commit 89d416b

Please sign in to comment.