Petite-Vue on-click missing in compiled html, and "not defined" for reactive properties in app

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

Petite-Vue on-click missing in compiled html, and "not defined" for reactive properties in app

问题

我尝试使用v-if来切换div的可见性,以创建一个“假”多页面网站。然而,我遇到了两个问题:

首先,<a href="#" v-on:click="toggleText">click to toggle</a> 编译为 <a href="#">click to toggle</a>。同样,当我使用div而不是a href时,它也无法被点击。

其次,即使我将div切换为按钮,我仍然收到一个错误消息“showText未定义”,但我在app对象中定义了ShowText作为一个响应式属性。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Petite-Vue Example</title>
  <style>
    .hidden-text {
      display: none;
    }
  </style>
</head>
<body>
  <div id="app">
    <button class="clickable" @click="toggleText">Click me!</button>
    <div v-if="showText" class="hidden-text">Hidden text revealed!</div>
  </div>

  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue@0.4.1/dist/petite-vue.es.js?module'
    const app = {
      showText: false,
      toggleText() {
        this.showText = !this.showText;
      }
    };

    createApp({
      data() {
        return app;
      },
      methods: {
        toggleText: app.toggleText
      }
    }).mount('#app');
  </script>
</body>
</html>

希望这些指针对你有帮助!

英文:

I am trying to create a "fake" multi-page website by toggling div visibility using v-if. However I am running into two issues:

First &lt;a href=&quot;#&quot; v-on:click=&quot;toggleText&quot;&gt;click to toggle&lt;/a&gt; is being compiled to &lt;a href=&quot;#&quot;&gt;click to toggle&lt;/a&gt;. Same when using div instead of a href, and then it can not be clicked.

Secondly, even if I switch the div to a button I keep getting an error "showText is not defined", but I define ShowText as a reactive property in the app object?

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;title&gt;Petite-Vue Example&lt;/title&gt;
  &lt;style&gt;
    .hidden-text {
      display: none;
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div id=&quot;app&quot;&gt;
    &lt;button class=&quot;clickable&quot; @click=&quot;toggleText&quot;&gt;Click me!&lt;/button&gt;
    &lt;div v-if=&quot;showText&quot; class=&quot;hidden-text&quot;&gt;Hidden text revealed!&lt;/div&gt;
  &lt;/div&gt;

  &lt;script type=&quot;module&quot;&gt;
    import { createApp } from &#39;https://unpkg.com/petite-vue@0.4.1/dist/petite-vue.es.js?module&#39;
    const app = {
      showText: false,
      toggleText() {
        this.showText = !this.showText;
      }
    };

    createApp({
      data() {
        return app;
      },
      methods: {
        toggleText: app.toggleText
      }
    }).mount(&#39;#app&#39;);
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Any pointers would be appreciated!

答案1

得分: 3

以下是翻译好的内容:

事件处理程序不会出现在渲染后的 DOM HTML 中,因为 HTML 规范中没有支持它们,所以这是预期的。像这样的 Vue 特定语法在编译为浏览器时会被剥离并转换为 JavaScript。

您的问题在于您正在使用普通的 Vue Options API,而 petite-vue 不支持它。请仔细阅读文档和示例代码。您会看到没有 data()methods 等选项。

您的应用应该写成:

createApp({
  showText: false,
  toggleText() {
    this.showText = !this.showText;
  }
}).mount('#app');

jsfiddle

请注意,我已经从 div 元素中删除了 hidden-text 类,因为display: none 会始终隐藏文本,不管 v-if 的结果如何。

英文:

Event handlers don't appear in rendered DOM HTML as there is nothing in the HTML spec that supports them, so that's expected. Vue specific syntax like that gets stripped away and turned into JavaScript when it's compiled for the browser.

Your issue is that you're using normal Vue Options API which petite-vue doesn't support. Read carefully through the documentation and example code. You'll see there is no data(), methods, etc. options.

Your app should be written as:

createApp({
  showText: false,
  toggleText() {
    this.showText = !this.showText;
  }
}).mount(&#39;#app&#39;);

jsfiddle

Note I removed the hidden-text class on the div element because display: none would always hide the text regardless of the v-if result.

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

发表评论

匿名网友

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

确定