放大器状态将被忽略,直到触发setState()

时间:2019-07-04 07:36:57

标签: javascript html amp-html

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

在我点击<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button>之前,amp文档中的所有示例都无法使用。然后触发计算,页面显示所有值。在此之前,没有插值/计算任何值。

我在这里做什么错了?

enter image description here

<!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>

1 个答案:

答案 0 :(得分:2)

如果我的理解正确,那么您面临的问题是功放绑定仅根据用户操作来计算。您必须初始化初始值服务器端。绑定就是这样工作的。这样做的目的是确保页面速度,并且在页面加载时无需运行JavaScript。

看到类似的代码很常见

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

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

但是,我可以看到AMP Geo产生的问题。服务器是否应该复制该功能以预先初始化值?例如,请参见https://github.com/ampproject/amphtml/issues/14637。听起来就像您面临的同样问题。您可能想在此处添加评论,以提供有关为什么要添加某种支持的反馈。