created() { //this.bg(); let type = this.$route.query.type ? this.$route.query.type : ''; this.init(type); this.baseUrl = this.$config.baseUrl; }, methods: { getMakeZero(s) { return s < 10 ? '0' + s : s; }, // 下载 download(file){ window.open(`${file}`) }, // 初始化 init(type) { this.type = type; if(type=='cross'){ var obj = JSON.parse(localStorage.getItem('crossObj')); for (var o in obj){ if(o=='shiciming'){ this.ruleForm.shiciming = obj[o]; this.ro.shiciming = true; continue; } if(o=='fengmian'){ this.ruleForm.fengmian = obj[o].split(",")[0]; this.ro.fengmian = true; continue; } if(o=='shici'){ this.ruleForm.shici = obj[o]; this.ro.shici = true; continue; } if(o=='jiexi'){ this.ruleForm.jiexi = obj[o]; this.ro.jiexi = true; continue; } } } // 获取用户信息 this.$http.get(this.userTableName + '/session', {emulateJSON: true}).then(res => { if (res.data.code == 0) { var json = res.data.data; } }); }, // 多级联动参数 // 多级联动参数 info(id) { this.$http.get('jinrishici/detail/${id}', {emulateJSON: true}).then(res => { if (res.data.code == 0) { this.ruleForm = res.data.data; } }); }, // 提交 onSubmit() {
时间: 2023-12-30 09:04:58 浏览: 26
这段代码是一个 Vue.js 组件中的方法,其中包含了一些常见的 Vue.js 方法,例如 `created()` 和 `methods`。在 `created()` 钩子函数中,通过读取路由参数来初始化组件,并设置了一个 `baseUrl` 变量。在 `methods` 对象中定义了一些方法,例如 `getMakeZero()` 用于将数字转换为两位数,`download()` 用于下载文件,`init()` 用于初始化组件,`info()` 用于获取多级联动参数,`onSubmit()` 则用于提交表单。这段代码中还使用了 `this.$http` 来进行网络请求,其中 `get()` 方法用于发送 GET 请求。
相关问题
this.$route.path报错
这个错误通常是因为你在组件中使用了 `this.$route.path`,但是没有在组件中导入 `vue-router`。你需要确保在组件中导入 `vue-router` 并在 `created` 生命周期函数中使用 `$route.path`。具体的代码示例如下:
```
<template>
<div>{{ currentPath }}</div>
</template>
<script>
import { mapState } from 'vuex'
import { VueRouter } from 'vue-router'
export default {
name: 'MyComponent',
mixins: [VueRouter],
created() {
this.currentPath = this.$route.path
},
computed: {
...mapState(['currentPath'])
}
}
</script>
```
如果你已经导入 `vue-router` 并且仍然遇到问题,请检查是否正确安装了 `vue-router`。你可以在终端中运行以下命令安装 `vue-router`:
```
npm install vue-router
```
this.$route.params无法获取到参数值
如果使用this.$route.params无法获取到参数值,可能有以下几种情况:
1. 参数未正确传递:请确保在页面跳转时,参数被正确地传递给了目标页面。例如,使用this.$router.push方法时,检查参数是否正确地包含在目标路由的路径中。
2. 路由配置错误:请检查Vue Router的路由配置是否正确。确保在路由配置中指定了需要接收参数的动态路由路径,并按照正确的方式进行定义。例如,使用冒号(:)来指定动态参数。
3. 组件生命周期问题:请确保在需要访问参数的组件的生命周期钩子函数中进行参数的获取操作。通常,params参数只能在created和mounted等生命周期钩子函数中才能获取到。
4. 路由模式不匹配:如果使用了history模式的路由,并且在刷新页面或直接访问特定路由时无法获取参数值,可能是后端服务器未正确配置重定向规则导致的。
如果以上情况都排除了,仍然无法获取参数值,可以进一步检查控制台输出是否有相关的错误信息,或者尝试使用调试工具(如Vue Devtools)来查看$route对象的具体内容,以确定问题所在。