Vue在渲染2个cshtml页面后停止工作的原因是什么?

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

why does vue stopped working after rendering 2 pages in cshtml?

问题

I am extremely new to programming and i am trying to build a website with asp.net mvc.

after rendering a view page to _layout the vue in the _layout stopped working and the console shows this message

Uncaught SyntaxError: Identifier 'app' has already been declared (at (index):562:13)

I think it is caused by me applying vue scripts to both pages, but i really have no idea how to solve this situation with my very limited knowledge. i have the same code like the following for both cshtml files(_layout and the inner page)

const app = Vue.createApp({
data() { return { }; },
methods: { mymethodshere } })
.mount("#app");

Is there a way to get both pages running smoothly?

thanks for the help in advance.

I have tried to give use different id for the vue to mount, but that doesn't seem to fix the issue.

英文:

I am extremely new to programming and i am trying to build a website with asp.net mvc.

after rendering a view page to _layout the vue in the _layout stopped working and the console shows this message

Uncaught SyntaxError: Identifier 'app' has already been declared (at (index):562:13)

I think it is caused by me applying vue scripts to both pages, but i really have no idea how to solve this situation with my very limited knowledge. i have the same code like the following for both cshtml files(_layout and the inner page)

const app = Vue.createApp({ 
   data() { return { }; }, 
   methods: { mymethodshere } })
.mount("#app");

Is there a way to get both pages running smoothly?

thanks for the help in advance.

I have tried to give use different id for the vue to mount, but that doesn't seem to fix the issue.

答案1

得分: 1

如错误提示所述,您在页面中两次使用了Vue的app

_layout和其他地方,您都使用了相同的app变量。

const app = Vue.createApp

您可以将它们重命名为app1app2,例如:

const app1 = Vue.createApp
.mount("#app1");

const app2 = Vue.createApp
.mount("#app2");

或者您可以将它们合并为一个Vue应用程序。

英文:

As the error stated, you have your Vue app twice in the page.

In _layout and somewhere else you have the same app variable.

const app = Vue.createApp

You can for example rename them to app1 and app2.

const app1 = Vue.createApp
.mount("#app1");

const app2 = Vue.createApp
.mount("#app2");

Or you have to combine both to one Vue app.

huangapple
  • 本文由 发表于 2023年5月11日 08:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223436.html
匿名

发表评论

匿名网友

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

确定