Type error in TypeScript with Vue.js and Data binding

huangapple go评论76阅读模式
英文:

Type error in Type-Script with Vue.js and Databinding

问题

对不起,你的要求是只返回翻译好的部分,不包括代码。以下是你的原文的翻译:

我在Java/TypeScript领域相对陌生...

我想执行一个简单的任务,目前已经花了我三天以上的开发时间。我甚至不确定为什么会出现这种情况,因为编译和构建都没有出现错误。

我正在使用vue.js 3:

模板中的错误:

<script setup lang="ts">

import type {CompanyPojo} from "@/rest/restapi-companies";
import {createNewCompany} from "@/rest/restapi-companies";

let companyPojo: CompanyPojo;

</script>


<template>

  <input id="cName" v-model="companyPojo.name" placeholder="Company Name"/>
  <label for="cName">Company Name</label>

<!--<input id="cAddName" v-model="companyPojo.additionalName" placeholder="Company Additional Name"/>-->
<!--<label for="cAddName">Company Additional Name</label>-->


<!--<input id="cLegalForm" v-model="companyPojo.legalForm" placeholder="Company Legal Form"/>-->
<!--<label for="cLegalForm">Company Legal Form</label>-->

  <button @click="createNewCompany(Object.assign(companyPojo))"> Add Company
  </button>


</template>

这是我的TypeScript文件:

export type CompanyPojo = {
    id: number;
    name: string;
    legalForm: string;
    additionalName: string;
};

export type CompanyPojoResponse = {
    data: CompanyPojo[];
};

export async function createNewCompany(newCompany: CompanyPojo) {

    try {
        const {data, status} = await axios.post<CompanyPojoResponse>('http://localhost:9050/api/v1/company/create-new', newCompany,
            {
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: 'Basic XXXXXXXXXXXXXXXXXX',
                },
            },
        );

        console.log(JSON.stringify(data, null, 4));

        // ?? "response status is: 200"
        console.log('response status is: ', status);

        return data;
    } catch (error) {
        if (axios.isAxiosError(error)) {
            console.log('error message: ', error.message);
            console.log('error message: ', error);
            return error.message;
        } else {
            console.log('unexpected error: ', error);
            return 'An unexpected error occurred';
        }
    }


}

当我导入这些导出的文件时,我期望所有这些类型都会可用(在开发和启动时确实是如此)。

但是,当我打开New-Company View时,视图为空,我在控制台中看到这个错误:

TypeError: (intermediate value)(...) is undefined
setup http://localhost:9050/assets/CompanyNewView-215ff09f.js:1

删除以下部分:

<input id="cName" v-model="companyPojo.name" placeholder="Company Name"/>
<label for="cName">Company Name</label>

解决了这个问题。这真的让我抓狂,我怎么能绑定和使用这些值呢?
我甚至不知道为什么这根本不起作用...

英文:

i am pretty fresh in the world of java/type script....

I want to perform a simple task, which currently costs me more than three days in development. I am even not sure why this does happen, since compiling and building are running without an error.

I am using vue.js3:

The Template with error:

&lt;script setup lang=&quot;ts&quot;&gt;

import type {CompanyPojo} from &quot;@/rest/restapi-companies&quot;;
import {createNewCompany} from &quot;@/rest/restapi-companies&quot;;

let companyPojo: CompanyPojo;

&lt;/script&gt;


&lt;template&gt;

  &lt;input id=&quot;cName&quot; v-model=&quot;companyPojo.name&quot; placeholder=&quot;Company Name&quot;/&gt;
  &lt;label for=&quot;cName&quot;&gt;Company Name&lt;/label&gt;

&lt;!--  &lt;input id=&quot;cAddName&quot; v-model=&quot;companyPojo.additionalName&quot; placeholder=&quot;Company Additional Name&quot;/&gt;--&gt;
&lt;!--  &lt;label for=&quot;cAddName&quot;&gt;Company Additional Name&lt;/label&gt;--&gt;


&lt;!--  &lt;input id=&quot;cLegalForm&quot; v-model=&quot;companyPojo.legalForm&quot; placeholder=&quot;Company Legal Form&quot;/&gt;--&gt;
&lt;!--  &lt;label for=&quot;cLegalForm&quot;&gt;Company Legal Form&lt;/label&gt;--&gt;

  &lt;button @click=&quot;createNewCompany(Object.assign(companyPojo))&quot;&gt; Add Company
  &lt;/button&gt;


&lt;/template&gt;

and this is my TypeScript-File:

export type CompanyPojo = {
    id: number;
    name: string;
    legalForm: string;
    additionalName: string;
};

export type CompanyPojoResponse = {
    data: CompanyPojo[];
};

export async function createNewCompany(newCompany: CompanyPojo) {

    try {
        const {data, status} = await axios.post&lt;CompanyPojoResponse&gt;(&#39;http://localhost:9050/api/v1/company/create-new&#39;, newCompany,
            {
                headers: {
                    &#39;Content-Type&#39;: &#39;application/json&#39;,
                    Authorization: &#39;Basic XXXXXXXXXXXXXXXXXX&#39;,
                },
            },
        );

        console.log(JSON.stringify(data, null, 4));

        // ?? &quot;response status is: 200&quot;
        console.log(&#39;response status is: &#39;, status);

        return data;
    } catch (error) {
        if (axios.isAxiosError(error)) {
            console.log(&#39;error message: &#39;, error.message);
            console.log(&#39;error message: &#39;, error);
            return error.message;
        } else {
            console.log(&#39;unexpected error: &#39;, error);
            return &#39;An unexpected error occurred&#39;;
        }
    }


}

As i imported the exported files, i expected that all those types would be availble (which there are in dev and start time).

But then, simply when opening the New-Company View, the view is empty and i see in the console this error:

TypeError: (intermediate value)(...) is undefined
setup http://localhost:9050/assets/CompanyNewView-215ff09f.js:1

Removing:

 &lt;input id=&quot;cName&quot; v-model=&quot;companyPojo.name&quot; placeholder=&quot;Company Name&quot;/&gt;
  &lt;label for=&quot;cName&quot;&gt;Company Name&lt;/label&gt;

Solves to problem.

This driving me nuts, how in the world i can bind and use those value?
I even don't know why this is not working at all.....

答案1

得分: 1

你的数据应该是响应式的,以便正确绑定,请尝试使用 ref 来创建你的数据属性:

<script setup lang="ts">
import { ref } from 'vue';
import type { CompanyPojo } from "@/rest/restapi-companies";
import { createNewCompany } from "@/rest/restapi-companies";

const companyPojo = ref<CompanyPojo>({});
</script>
英文:

Your data should be reactive in order to be bound correctly try to use ref to create your data property :

&lt;script setup lang=&quot;ts&quot;&gt;
import {ref} from &#39;vue&#39;
import type {CompanyPojo} from &quot;@/rest/restapi-companies&quot;;
import {createNewCompany} from &quot;@/rest/restapi-companies&quot;;

const companyPojo = ref&lt;CompanyPojo&gt;({});

&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年3月7日 05:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定