【问题标题】:Amp-state getting ignored until setState() fired在 setState() 触发之前,放大器状态一直被忽略
【发布时间】:2019-07-04 07:36:57
【问题描述】:

我正在构建 amp 页面,该页面应根据国家/地区显示不同的下载 url。通过将isEU 组添加到<amp-geo> 配置,我可以通过css 显示/隐藏正确的<a> 元素。但我想要的只是页面上一个带有可变href的链接。我试图通过使用来实现这一点,但我无法使用<amp-state>,尽管它是正确生成的。

在我点击<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button> 之前,即使是 amp 文档中的示例也不起作用。然后它触发计算并且页面显示所有值。在此之前没有插值/计算值。

我在这里做错了什么?

<!doctype html>
<html ⚡>

<head>
    <meta charset="utf-8">
    <title>TESTPAGE</title>
    <meta name="viewport"
          content="width=device-width,minimum-scale=1,initial-scale=1">
    <script async
            src="https://cdn.ampproject.org/v0.js"></script>
    <script async
            custom-element="amp-geo"
            src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>
    <script async
            custom-element="amp-bind"
            src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
    <style>
        [class*=-only] {
            display: none;
        }

        .amp-geo-group-isEU .eu-only {
            display: block;
        }

        body:not(.amp-geo-group-isEU) .non-eu-only {
            display: block;
        }
    </style>
</head>

<body class="amp-geo-pending">
    <amp-state id="stateTest"
               class="i-amphtml-element i-amphtml-layout-container"
               i-amphtml-layout="container"
               hidden=""
               aria-hidden="true">
        <script type="application/json">
            {
                "testInitialKey": "initial state value"
            }
        </script>
    </amp-state>
    <amp-state id="myCircle"
               class="i-amphtml-element i-amphtml-layout-container"
               i-amphtml-layout="container"
               hidden=""
               aria-hidden="true">
        <script type="application/json">
            {
                "radius": "4"
            }
        </script>
    </amp-state>
    <amp-geo layout="nodisplay">
        <script type="application/json">
            {
              "AmpBind": true,
              "ISOCountryGroups": {
                "isEU": ["at", "be", "bg", "bs", "ch", "cy", "cz", "de", "dk", "ee", "es", "fi", "fr", "gb", "gr", "hr", "hu", "ie", "is", "it", "li", "lt", "lu", "lv", "mt", "nl", "no", "pl", "pt", "ro", "se", "si", "sk"]
              }
            }
        </script>
    </amp-geo>
    <h1 [class]="ampGeo.isEU ? 'isEU' : 'nonEU'"
        [text]="'isEU: ' + (ampGeo.isEU ? 'true' : 'false').toUpperCase()"></h1>
    <h2 [text]="stateTest.testInitialKey"></h2>
    <a class="eu-only"
       [href]="'http://google.com/' + ampGeo.ISOCountry == 'cy' ? 'cyprusLink' : 'defaultLink'">LINK_CY</a>
    <a class="non-eu-only"
       href="http://google.com/?frenchLink">LINK_FR</a>
    <hr>
    <amp-bind-macro id="circleArea"
                    arguments="radius"
                    expression="3.14 * radius * radius"></amp-bind-macro>

    <div>
        The circle has an area of <span [text]="circleArea(myCircle.radius)">0</span>.
    </div>
    <hr>
    <p [text]="'Hello ' + foo">Hello World</p>
    <button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button>
</body>

</html>

【问题讨论】:

    标签: javascript html amp-html


    【解决方案1】:

    如果我理解正确,您面临的问题是 amp-bind 仅根据用户操作计算。您必须初始化初始值服务器端。这就是绑定的工作原理。这个想法是确保页面速度,并且在页面加载时不需要运行 JavaScript。

    这样的代码很常见

    <div class="foo" [class]="x ? 'foo' : 'bar'">
    

    即初始化类属性服务器端,然后在用户交互时动态更新它,这可能导致状态变化。

    但是,我可以看到这与 AMP Geo 产生的问题。服务器是否应该复制该功能以预初始化值?例如,请参阅https://github.com/ampproject/amphtml/issues/14637。听起来像你面临的同样的问题。您可能想在此处添加评论,以提供反馈,说明您希望添加某种支持的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      相关资源
      最近更新 更多