簡體   English   中英

模板拋出-未捕獲的TypeError:無法調用未定義的方法“ match”

[英]Template throwing - Uncaught TypeError: Cannot call method 'match' of undefined

我的html中有以下模板

<script id="mapview-tpl" type="text/x-handlebars-template">
<div class='header'><a href='#' class="button header-button header-button-left">Back</a><h1>Map</h1></div>
 <div class='details'>
    <script type="text/javascript">
        function initialize() {
            var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644),
            zoom: 8
            };
            var map = new google.maps.Map(document.getElementById("map-canvas"),
                mapOptions);
        }
        google.maps.event.addDomListener(window, 'load', initialize);
        </script>
<div id="map-canvas"/>


</div>

並且我具有使用以下功能將視圖從主視圖“轉換”到此視圖的功能

route: function() {
        var self = this;
        var hash = window.location.hash;
        if (!hash) {
            if (this.homePage) {
                this.slidePage(this.homePage);
            }else {
                this.homePage = new HomeView(this.store).render();
                this.slidePage(this.homePage);
            }
            return;
        }
        var match = hash.match(this.detailsURL);
        if (match) {
            this.store.findById(Number(match[1]), function(employee) {
                self.slidePage(new EmployeeView(employee).render());
            });
        }

        match = hash.match(this.mapsURL);
        if (match) {

            this.store.findById(Number(match[1]), function(employee) {
                self.slidePage(new MapView(employee).render());
            });
        }
    },

並且到達上述函數的結尾時,將引發以下錯誤Uncaught TypeError: Cannot call method 'match' of undefined hmm的Uncaught TypeError: Cannot call method 'match' of undefined嗎?

======

添加

我忘了補充說這個錯誤只在調用(this.mapsURL)的意義時拋出,它只會在

match = hash.match(this.mapsURL);
    if (match) {

        this.store.findById(Number(match[1]), function(employee) {
            self.slidePage(new MapView(employee).render());
        });
    }

叫做。 如果在上面,則工作正常

對於這種情況,我通常使用test ,它在undefined值的情況下更安全: (this.detailsURL).test(hash)(this.mapsURL).test(hash)

在您的情況下,無論如何我也將檢查hash值以了解發生了什么:如果沒有哈希,則應為“”,但它甚至不是undefined -否則它應該輸入您的第一個條件並返回我猜。

傻我! 發現這可能是模板無法編譯的“通用”錯誤,在我的情況下,模板名稱定義錯誤! 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM